ggplot2:标记饼图问题

时间:2018-07-17 23:00:59

标签: r ggplot2 pie-chart

我尝试使用geom_label_repel()包中的ggrepel来标记饼图,但无法正常工作。馅饼的颜色消失了。

我的脚本:

ggplot(HumanBittingRates,
       aes(x=factor(1), y = HBRs.Per.Person.Per.Night, fill = factor(Type))) + 
  geom_bar(width = 1, stat = "identity", position = "fill") + 
  facet_wrap(~Area) + 
  coord_polar(theta = "y")

enter image description here

但是当我添加标签

ggplot(HumanBittingRates,
       aes(x=factor(1), y = HBRs.Per.Person.Per.Night, fill = factor(Type))) + 
  geom_bar(width = 1, stat = "identity", position = "fill") + 
  facet_wrap(~Area) + 
  coord_polar(theta = "y") + 
  geom_label_repel(aes(y = HBRs.Per.Person.Per.Night, label = Lable),
                   size = 2, show.legend = F, nudge_x = 1)

情节看起来像这样:

enter image description here

我的数据:

Area    Type    HBRs Per Person Per Night   Lable
Biron Badala    An. gambiaes.l.     17.92   85.25%
Biron Badala    An. nili    2.1 9.99%
Biron Badala    An. coustani    0.57    2.71%
Biron Badala    An. funestus    0.43    2.05%
Cisse   An. gambiaes.l.     27.81   98.58%
Cisse   An. nili    0.06    0.21%
Cisse   An. coustani    0.27    0.96%
Cisse   An. funestus    0.07    0.25%
Kamadena    An. gambiaes.l.     21.6    96.69%
Kamadena    An. nili    0.29    1.30%
Kamadena    An. coustani    0.35    1.57%
Kamadena    An. funestus    0.1 0.45%
Kodougou    An. gambiaes.l.     21.56   92.14%
Kodougou    An. nili    1.31    5.60%
Kodougou    An. coustani    0.41    1.75%
Kodougou    An. funestus    0.12    0.51%
Konkuini    An. gambiaes.l.     22.42   97.99%
Konkuini    An. nili    0.04    0.17%
Konkuini    An. coustani    0.32    1.40%
Konkuini    An. funestus    0.1 0.44%
Labarani    An. gambiaes.l.     22.63   95.36%
Labarani    An. nili    0.74    3.12%
Labarani    An. coustani    0.33    1.39%
Labarani    An. funestus    0.03    0.13%

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我之前已经回答过similar question。造成这种现象的原因是position = "fill"选项创建的条形之间的断开连接,这些条形的高度总计为1,而文本标签的y值大得多。

我正在创建一个新答案,而不是将其重复投票,因为我的较早解决方案无法直接在此处应用:ggrepel包的geom_label_repel函数(至今)还不了解position = ""参数

library(dplyr)

df2 <- HumanBittingRates %>%
  group_by(Area) %>%
  arrange(desc(Type)) %>%
  mutate(y = HBRs.Per.Person.Per.Night / sum(HBRs.Per.Person.Per.Night),
         y.stack = cumsum(y)) %>%
  ungroup()

> df2
# A tibble: 24 x 6
   Area         Type           HBRs.Per.Person.Per.Night Lable        y y.stack
   <fctr>       <fctr>                             <dbl> <fctr>   <dbl>   <dbl>
 1 Biron_Badala An.nili                           2.10   9.99%  0.0999  0.0999 
 2 Cisse        An.nili                           0.0600 0.21%  0.00213 0.00213
 3 Kamadena     An.nili                           0.290  1.30%  0.0130  0.0130 
 4 Kodougou     An.nili                           1.31   5.60%  0.0560  0.0560 
 5 Konkuini     An.nili                           0.0400 0.17%  0.00175 0.00175
 6 Labarani     An.nili                           0.740  3.12%  0.0312  0.0312 
 7 Biron_Badala An.gambiaes.l.                   17.9    85.25% 0.853   0.952  
 8 Cisse        An.gambiaes.l.                   27.8    98.58% 0.986   0.988  
 9 Kamadena     An.gambiaes.l.                   21.6    96.69% 0.967   0.980  
10 Kodougou     An.gambiaes.l.                   21.6    92.14% 0.921   0.977  
# ... with 14 more rows

上面的代码创建了两个附加列:y返回每个条形的缩放高度,中每个类型,而y.stack返回每个标签的缩放和堆叠高度,按照堆叠的条形的顺序排序。

这样,您可以绘制所需的饼图:

ggplot(df2,
       aes(x = factor(1), fill = factor(Type), label = Lable)) + 
  geom_col(aes(y = y), width = 1) + # geom_col() is equivalent to geom_bar(stat = "identity")
  geom_label_repel(aes(y = y.stack), size = 2, show.legend = F, nudge_x = 1) +
  facet_wrap(~ Area) + 
  coord_polar(theta = "y") +
  theme_void() # for cleaner look

plot

相关问题