2

My df looks like:

1   with score
125 without score

I'd like to make piechart in ggplot:

ggplot(data = df_pp, aes(x = "", y = Number/sum(Number),fill =     PolyPhen_score)) + geom_bar(stat="identity") + coord_polar(theta='y')  +
ggtitle("Variants with PolyPhen score and without PolyPhen score") + 
scale_y_continuous(labels  = percent) + labs(x = "", y = "") +
theme(panel.background = element_rect(fill = 'white')) + 
geom_text(aes(label = paste(round(Number/sum(Number)*100),"%")),vjust = 5)

But my labels 1% and 99% are overlapped on the piechart.

If I add this code, it doesn't help:

geom_text(aes(y=Number/2 + c(0,cumsum(Number)[-length(Number)]), label = percent(Number/100)), size=5)
zx8754
  • 52,746
  • 12
  • 114
  • 209
Anastasia
  • 27
  • 1
  • 2
  • 6

1 Answers1

2

Maybe these other Stackoverflow questions can help you:

  1. ggplot, facet, piechart: placing text in the middle of piechart slices
  2. R + ggplot2 => add labels on facet pie chart

I did few tests on my machine with some random data and I tried with this:

geom_text(aes(x = 1.7, y=cumsum(Number/sum(Number)) - Number/sum(Number)/2, label = paste(round(Number/sum(Number)*100),"%")), size = 6)

X is the distance of labels from the center of the pie. x = 1.7 gives me a nice result with my data in RStudio, but probably you will need different values.

Community
  • 1
  • 1
mucio
  • 7,014
  • 1
  • 21
  • 33