多层饼图ggplot:标签重叠和图例

时间:2018-09-11 10:17:09

标签: r ggplot2 pie-chart

我有以下数据集:

data <- structure(list(Year = structure(c(1L, 2L, 1L, 2L, 2L, 1L, 3L, 1L, 3L, 1L, 2L, 2L, 3L, 2L, 3L, 
                                          1L, 3L, 2L, 2L, 2L, 3L, 1L, 2L, 3L, 3L, 3L, 2L, 1L, 3L, 1L, 
                                          1L, 2L, 1L, 2L, 3L, 2L, 2L, 1L, 1L, 3L, 3L, 3L, 3L, 1L, 3L, 
                                          3L, 2L, 3L, 2L, 1L, 1L, 2L, 2L, 1L), 
                                        .Label = c("2013", "2014", "2015"), 
                                        class = "factor"), 
                       Place = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 
                                           2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 
                                           2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 
                                           2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L), 
                                         .Label = c("Inside", "Outside"), 
                                         class = "factor"), 
                       Variable = structure(c(6L, 4L, 8L, 13L, 16L, 11L, 12L, 13L, 4L, 10L, 10L, 11L, 
                                              1L, 3L, 13L, 7L, 11L, 7L, 6L, 2L, 6L, 1L, 1L, 7L, 5L, 
                                              3L, 14L, 3L, 14L, 2L, 9L, 6L, 6L, 9L, 2L, 5L, 9L, 5L, 
                                              9L, 9L, 15L, 1L, 13L, 3L, 6L, 3L, 3L, 9L, 15L, 1L, 13L, 
                                              1L, 13L, 15L), 
                                            .Label = c("X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", 
                                                       "Y1", "Y2", "Y3", "Y4", "Y5", "Y6", "Y7", "Y8"), 
                                            class = "factor"), 
                       Group = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 
                                           2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 
                                           1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 
                                           2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L), 
                                         .Label = c("Var1", "Var2"), 
                                         class = "factor"), 
                       Percent = c(0.2, 0.3, 0.4, 0.5, 0.5, 0.6, 0.7, 0.7, 1.3, 1.6, 1.9, 1.9, 2.3, 2.7, 
                                   2.9, 3.4, 3.7, 4.5, 4.7, 5.3, 5.7, 6.2, 7.6, 7.9, 10.6, 10.7, 12.5, 
                                   13.3, 14.4, 15.4, 15.8, 16.9, 17.7, 19.6, 20.5, 24.8, 25.3, 30.4, 31, 
                                   36.8, 41.6, 43.9, 43.9, 44.2, 45.4, 51.8, 52.8, 56.1, 57.4, 68.9, 68.9, 
                                   80.4, 80.4, 81.5)), 
                  class = "data.frame", row.names = c(NA, -54L))

我真的很想以这样的多层次显示数据:

Example Pie Chart

我尝试这样做:

library(ggplot2)

ggplot(data, aes(x = Group, y = Percent, fill = Variable)) +
  geom_bar(stat = "identity", position = "fill") +
  facet_grid(Year ~ Place) +
  geom_text(aes(label= paste(Percent, "%", Variable)) ,
            position = position_fill(0.9), size = 3)+
  coord_polar(theta = "y")

Pie Chart Created with overlapping labels

但是由于某些百分比非常低,因此各层重叠。如果可能,我想将标签放在示例之外。

我看了其他论坛主题,但是由于我的数据结构不同,所以无法为我翻译。另一个问题是,这只是一个示例,但是我的数据输入实际上取决于Shiny输入。因此,在此示例中固定特定角度也不起作用。

如果有人能帮助我,我将不胜感激。

0 个答案:

没有答案