Home > Back-end >  R: One label situated wrong
R: One label situated wrong

Time:08-09

I have made the following plot using this code,

pie chart with bad label

however as u can see in the picture, the label for "Mittlere Reife" (16,2%) is not where its supposed to be. Every other label is situated perfectly, and I cant figure out why the one just won´t work. Thanks for your help ;)

Editdput(Bildung):

Bildung <- structure(list(Q4 = structure(c(1L, 2L, 3L, 5L, 4L), .Label = c("Andere", 
"Qualifizierter Hauptschulabschluss", "Mittlere Reife", "Fachabitur/Allgemeines Abitur", 
"Universitätsabschluss"), class = "factor"), n = c(1L, 5L, 42L, 
72L, 139L), perc = c(0.00386100386100386, 0.0193050193050193, 
0.162162162162162, 0.277992277992278, 0.536679536679537), labels = c("0.4%", 
"1.9%", "16.2%", "27.8%", "53.7%")), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))    

ggplot(Bildung, aes(x="", y= perc, fill=Q4))  
  geom_bar(stat="identity", width=1)  
  geom_label(aes(label = labels),
             position = position_dodge2(width =1),
             show.legend = FALSE)  
  coord_polar("y", start=0) 
  theme_void() 
  labs(fill = "Bildungsgrad") 
  scale_fill_brewer(palette = "Blues") 
  theme(legend.key.size = unit(1, 'cm'))

CodePudding user response:

If you want the labels in the center of the slices, you need to calculate those values yourself. Here's one way to do this using dplyr

Bildung %>% 
  arrange(desc(Q4)) %>% 
  mutate(midpoint = cumsum(perc)-perc/2) %>% 
 ggplot(aes(x=1, y=perc, fill=Q4))  
  geom_col(width=1)  
  geom_label(aes(label = labels,  x=seq(1.5,.7,length.out=5), y=midpoint),
             show.legend = FALSE)  
  coord_polar("y", start=0) 
  theme_void() 
  labs(fill = "Bildungsgrad") 
  scale_fill_brewer(palette = "Blues") 
  theme(legend.key.size = unit(1, 'cm'))

This returns

labeled pie

  • Related