0

I have created some nice pie charts but am having difficulty adding % labels to the pie charts. Environment is Linux.

Input data is a tab delimited text file:

TIMEFRAME   POPULATION  AMOUNT
Deepest_Ancestral   African 0.06
Deepest_Ancestral   East_Asian  0.23
Deepest_Ancestral   European    0.71
Deeper_Ancestral    African 0.00
Deeper_Ancestral    East_Asian  0.40
Deeper_Ancestral    European    0.60
Ancestral   African 0.00
Ancestral   East_Asian  0.10
Ancestral   European    0.90

MY CODE:

library(ggplot2)
library(dplyr)

file_name <- "X3.txt"

#load file into data frame
test <- read.csv(file_name, sep="\t", header = TRUE)

ggsave("MultiPie.png")


ggplot(test, aes(x="", y=AMOUNT, group=POPULATION, color=POPULATION, fill=POPULATION)) +
  geom_bar(width = 1, size = 0.5, color = "white", stat = "identity") +
  geom_text(aes(label = AMOUNT), position = position_stack(vjust = 0.5)) +
  coord_polar(theta = "y") + 
  facet_wrap(~ TIMEFRAME, nrow = 2, ncol = 2) + 
  ggtitle("MUTATIONS YOU SHARE WITH VARIOUS POPULATIONS\n\n") +
  theme(plot.title = element_text(family = "Arial", color="black", face="bold", size=12, hjust=0.5)) +
  theme(legend.title = element_text(family = "Arial", color="black", face="bold", size=10, hjust=0)) +
  scale_fill_manual(values = c("red4", "gold1", "blue2")) +
  scale_color_manual(values = c("red4", "gold1", "blue2")) +
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        panel.grid  = element_blank(), 
        legend.background = element_rect(fill = "gray80"),
        plot.background = element_rect(fill = "gray70"),
        panel.background = element_rect(fill = "grey70"), 
        legend.position = "bottom", legend.justification = "center")

dev.off()

THE OUTPUT:

Multi-Pie

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Gene100
  • 121
  • 1
  • 4
  • 10
  • check out this [answer in SO](https://stackoverflow.com/questions/41338757/adding-percentage-labels-on-pie-chart-in-r). It might be what you're looking for – Wally Ali Nov 21 '18 at 04:03
  • 2
    Also check this article https://www.businessinsider.com/pie-charts-are-the-worst-2013-6 – atsyplenkov Nov 21 '18 at 10:39
  • I agree, however with a 4 or less pie chart with dissimilar values the pie is more visually appealing to me. – Gene100 Nov 21 '18 at 11:18
  • 1
    The rows of xxxx do give away the attention to detail and style. – hrbrmstr Nov 21 '18 at 12:54
  • @Gene100: see also this https://speakerdeck.com/cherdarchuk/data-looks-better-naked-pie-chart-edition – Tung Nov 21 '18 at 13:21

1 Answers1

2

Just change geom_text() to:

geom_text(aes(label = ifelse(AMOUNT == 0, "", paste0(100*AMOUNT, "%"))),
          position = position_stack(vjust = 0.5))

This will only display labels where your percents are larger than 0%, since those cases don't have a visible area in your plot.

alex_555
  • 1,092
  • 1
  • 14
  • 27
  • Thanks, works great, except I can't figure out why now an "a" is printed in the center of each of the 3 legend labels located at the bottom. Any idea on how to remove the letter "a" from each of those labels? – Gene100 Nov 21 '18 at 10:45
  • 1
    @Gene100 Set `show.legend = FALSE` in `geom_text`: `geom_text(aes(label = ifelse(AMOUNT == 0, "", paste0(100*AMOUNT, "%"))), position = position_stack(vjust = 0.5), show.legend = F)` – atsyplenkov Nov 21 '18 at 11:05