PalimPalim
PalimPalim

Reputation: 3068

ggplot label pie chart - next to pie pieces - legend incorrect

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.

Upvotes: 2

Views: 2277

Answers (1)

P1storius
P1storius

Reputation: 947

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)

Upvotes: 1

Related Questions