簡體   English   中英

向 ggplot2 中的餅圖添加百分比標簽?

[英]Add Percentage Labels to Pie chart in ggplot2?

我一直在努力嘗試在 ggplot2 的餅圖中放置百分比標簽。 我知道這已經發布了很多,但我無法使用示例來讓我的代碼工作。 我只是在尋找一種簡單有效的方法來添加百分比。 謝謝你。 代碼如下:

    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))
    '''

我的餅圖代碼如下:

   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()

在此處輸入圖像描述

再次感謝您花時間查看。

也許你可以試試這個:

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()

我希望它可以幫助你,保持我的狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM