Adding percentage labels on pie chart in R

前端 未结 5 1373
渐次进展
渐次进展 2020-12-03 19:23

My data frame looks like

df
   Group   value
1 Positive    52
2 Negative   239
3 Neutral     9

I would like to make a pie chart of the dat

5条回答
  •  星月不相逢
    2020-12-03 20:01

    For example, I create a dataframe e3 with 400 vehicles:

    e3 <- data.frame(400)
    e3 <- rep( c("car", "truck", "other", "bike", "suv"), c(60, 120, 20, 50, 150))
    

    Since pie charts are especially useful for proportions, let's have a look on the proportions of our vehicles, than we will report on the graph in this case:

    paste(prop.table(table(e3))*100, "%", sep = "")
    [1] "15%"   "5%"    "30%"   "12.5%" "37.5%"
    

    Then you can draw your pie chart,

    pie(table(e3), labels = paste(round(prop.table(table(e3))*100), "%", sep = ""), 
    col = heat.colors(5), main = "Vehicles proportions - n: 400")
    

提交回复
热议问题