简体   繁体   中英

ggplot2 : Labeling Pie Chart Issue

I try to label my pie chart using geom_label_repel() from the ggrepel package but it wouldn't work. The color of the pie was disappeared.

My script:

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")

在此处输入图片说明

But when I add a label

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)

the plot looks like this:

在此处输入图片说明

My data:

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%

Thanks for your help!!

I've answered a similar question before. The reason for this phenomenon is the disconnect between the position = "fill" option creating bars that add up to 1 in height, while the text labels have much larger y-values.

I'm creating a new answer instead of voting this as duplicate, because my earlier solution can't be applied here directly: ggrepel package's geom_label_repel function doesn't (yet) understand position = "" parameter.

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

The above code creates two additional columns: y returns the scaled height for each bar, within each Type, while y.stack returns the scaled AND stacked height for each label, sorted according to the order of the stacked bars.

With this, you can plot the desired pie charts:

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

情节

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM