2

I am using the following code

education_bachelor_summary<- education_bachelor %>%
  group_by(title_mapped) %>%
  summarise(n=n()) %>%
  mutate(perc = n / sum(n)) %>%
  arrange(desc(n)) %>%
  mutate(label=(str_c(title_mapped, "-", as.character(percent(perc)), sep=" ")))


midpoint <-sum(education_bachelor_summary$perc) - cumsum(education_bachelor_summary$perc) + education_bachelor_summary$perc/2

ggplot(education_bachelor_summary, aes(x = "", y=perc, fill = factor(title_mapped))) +
  geom_bar(width = 1, stat = "identity") +
  scale_y_continuous(breaks=midpoint, labels=education_bachelor_summary$label) +
  scale_fill_brewer(palette = "Blues", direction = -1) +
  labs(fill="Bachelor/ Vordiplom",x=NULL,y=NULL,title="",caption="") +
  coord_polar(theta = "y", start=0) +
  theme(axis.ticks = element_blank(), panel.grid  = element_blank(), axis.line = element_blank(), strip.background = element_blank(), panel.background = element_blank())

which generates the following plot enter image description here

Issues

  1. Labels next to pie pieces are cut off.
  2. labels on pie pieces are correct, but labels on right-hand side are mixed up, "Informatik" and "Mathe" are switched.

Code to reproduce example

library(tidyverse)
library(scales)
education_bachelor_summary <- structure(list(title_mapped = c("BWL","Mathe", "Informatik", "VWL", "Wirtschaftsingenieurwesen"), n = c(82L, 37L, 33L, 10L, 5L), perc = c(0.491017964071856, 0.221556886227545, 0.197604790419162, 0.0598802395209581, 0.029940119760479), label = c("BWL - 49.1%", "Mathe - 2.2%", "Informatik - 19.8%", "VWL - 6.0%", "Wirtschaftsingenieurwesen - 3.0%")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -5L))
midpoint <-sum(education_bachelor_summary$perc) - cumsum(education_bachelor_summary$perc) + education_bachelor_summary$perc/2

ggplot(education_bachelor_summary, aes(x = "", y=perc, fill = factor(title_mapped))) +
  geom_bar(width = 1, stat = "identity") +
  scale_y_continuous(breaks=midpoint, labels=education_bachelor_summary$label) +
  scale_fill_brewer(palette = "Blues", direction = -1) +
  labs(fill="Bachelor/ Vordiplom",x=NULL,y=NULL,title="",caption="") +
  coord_polar(theta = "y", start=0) +
  theme(axis.ticks = element_blank(), panel.grid  = element_blank(), axis.line = element_blank(), strip.background = element_blank(), panel.background = element_blank())

More generally I am looking to enhance my pie chart. I would like a pie chart with readable labels next to each piece of pie.

PalimPalim
  • 2,892
  • 1
  • 18
  • 40
  • 3
    Possible duplicate of [How can I put the labels outside of piechart?](https://stackoverflow.com/questions/48184645/how-can-i-put-the-labels-outside-of-piechart) – camille Sep 10 '18 at 14:09
  • I would disagree, I already have labels next to my pie chart. My main issue is that they are cut off and you cannot read the whole label. Something you can also see in the question's answer you linked. Should I rephrase to make this more clear? – PalimPalim Sep 10 '18 at 14:14
  • 1
    The accepted answer on that post doesn't spell it out, but they adjust the limits of both the x- and y-axes to make room for long text labels, which would likely solve your issue with labels being cut off – camille Sep 10 '18 at 14:17
  • Thanks I will try that, as soon as I am able to. – PalimPalim Sep 10 '18 at 14:17
  • Are you *sure* that the legend is wrong and your manually added chart labels are correct? – Z.Lin Sep 10 '18 at 14:57

1 Answers1

1

The color order is different between fill and legend, because you define them separately. The legend color is defined in scale_y_continuous(breaks=midpoint, labels=education_bachelor_summary$label), while the fill color depends on the order that factor(title_mapped) places them in. factor() orders them alphabetically by default if the input is a character vector.

To fix this, you can create a factor for fill value yourself as shown below.

library(tidyverse)
 library(scales)
 education_bachelor_summary <- structure(list(title_mapped = c("BWL","Mathe", "Informatik", "VWL", "Wirtschaftsingenieurwesen"), n = c(82L, 37L, 33L, 10L, 5L), perc = c(0.491017964071856, 0.221556886227545, 0.197604790419162, 0.0598802395209581, 0.029940119760479), label = c("BWL - 49.1%", "Mathe - 2.2%", "Informatik - 19.8%", "VWL - 6.0%", "Wirtschaftsingenieurwesen - 3.0%")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -5L))
 midpoint <-sum(education_bachelor_summary$perc) -  cumsum(education_bachelor_summary$perc) + education_bachelor_summary$perc/2
 education_bachelor_summary$fill <- factor(education_bachelor_summary$title_mapped, levels=education_bachelor_summary$title_mapped)

 ggplot(education_bachelor_summary, aes(x = "", y=perc, fill = fill)) +
  geom_bar(width = 1, stat = "identity") +
  scale_y_continuous(breaks=midpoint, labels=education_bachelor_summary$label) +
  scale_fill_brewer(palette = "Blues", direction = -1) +
  labs(fill="Bachelor/ Vordiplom",x=NULL,y=NULL,title="",caption="") +
  coord_polar(theta = "y", start=0) +
  theme(axis.ticks = element_blank(), panel.grid  = element_blank(), axis.line = element_blank(), strip.background = element_blank(), panel.background = element_blank())

In my own R session the labels are not cut off, so I'm afraid I cannot help you with that issue now. One thing you could try is looking at par(mar=c(...)) which is used to increase whitespace surrounding the plot window (see ?par)

P1storius
  • 917
  • 5
  • 12