1

I've been struggling with trying to put percentage labels on a pie chart in ggplot2. I know this has been posted a lot but I wasn't able to use examples to get my code to work. I'm just looking for a simple and a efficient way to add percentages. Thank you. The code is as follows:

    AccidentTypes <-read.csv(file.choose(),header=TRUE,sep=',')

    dput(AccidentTypes)

Output:

    '''
    structure(list(X0 = 1:7, AccidType = c("MotorVeh", "Poison", 
    "Drowning", "Fires", "Falls", "Firearms", "Other"), Deaths = c(10547L, 
    942L, 679L, 350L, 258L, 205L, 1074L)), class = "data.frame", row.names = c(NA, 
    -7L))
    '''

My code for the pie chart is as follows:

   ggplot(AccidentTypes,aes(x="",y=Deaths,fill=AccidType)) +
      geom_bar(stat='identity',width=1,color='white') +
      coord_polar('y',start=0) +
      ggtitle('Mortality Rates from Different Accident Types') +
      theme_void()

enter image description here

Thank you again for taking the time to look.

  • Does this answer your question? [How to create a pie chart with percentage labels using ggplot2?](https://stackoverflow.com/questions/45657990/how-to-create-a-pie-chart-with-percentage-labels-using-ggplot2) – Ian Campbell Jun 18 '20 at 15:06

1 Answers1

0

Maybe you could try this :

ggplot(AccidentTypes,aes(x="",y=Deaths,fill=AccidType)) +
      geom_bar(stat='identity',width=1,color='white') +
      coord_polar('y',start=0) +
      geom_text(aes(label=percent(Deaths/100)), position = position_stack(vjust=0.5), size= 3.5) +
      ggtitle('Mortality Rates from Different Accident Types') +
      theme_void()

I hope it can help you, keep me posted.

tiff_09
  • 1
  • 1
  • When I tried that, my percents didn't seem to be accurate. Also for the really small slices in the pie, when the percent labels tend to overlap where the portion is small, is there any way to fix that too? Thank you again. – Kyle Schichl Jun 18 '20 at 20:13
  • Concerning your overlap problem you can try this : `ggplot(AccidentTypes,aes(x="",y=Deaths,fill=AccidType)) + geom_bar(stat='identity',width=1,color='white') + coord_polar('y',start=0) + geom_text(aes(label=percent(Deaths/100)), position = position_stack(vjust=0.5), size= 3, angle=45) + ggtitle('Mortality Rates from Different Accident Types') + theme_void()` You can adjust the text size and text angle ... I hope it can help you. – tiff_09 Jun 22 '20 at 08:56