【问题标题】:How to plot pie chart in R from a table with relative Frequency?如何从具有相对频率的表中绘制 R 中的饼图?
【发布时间】:2017-01-17 02:32:59
【问题描述】:

我是 R 的新手。我需要绘制一个饼图。现在我已经尽力了,但它并没有为我生成饼图。下面是我的代码。

socialIssue <- matrix(c(245,112,153,71,133,306),ncol=1,byrow=T)
rownames(socialIssue) <- c("Housing","Transportation","Health Care","Education","Food","Other")
colnames(socialIssue) <- c("Frequency")
socialIssue <- as.table(socialIssue)
socialIssue/sum(socialIssue)

cols <- rainbow(nrow(socialIssue))
pie(socialIssue$Frequency, labels=paste0(round(socialIssue$Frequency/sum(socialIssue$Frequency)*100,2),"%"),colnames=cols)

这是以下输出。输出的频率是正确的。

socialIssue <- matrix(c(245,112,153,71,133,306),ncol=1,byrow=T)
> rownames(socialIssue) <- c("Housing","Transportation","Health Care","Education","Food","Other")
> colnames(socialIssue) <- c("Frequency")
> socialIssue <- as.table(socialIssue)
> socialIssue/sum(socialIssue)
                Frequency
Housing        0.24019608
Transportation 0.10980392
Health Care    0.15000000
Education      0.06960784
Food           0.13039216
Other          0.30000000
> 
> cols <- rainbow(nrow(socialIssue))
> pie(socialIssue$Frequency, labels=paste0(round(socialIssue$Frequency/sum(socialIssue$Frequency)*100,2),"%"),colnames=cols)
Error in socialIssue$Frequency : $ operator is invalid for atomic vectors

【问题讨论】:

    标签: r plot pie-chart frequency


    【解决方案1】:

    转换为数据框,然后绘图

    socialIssue = as.data.frame(socialIssue)
    socialIssue$percent = round(100*socialIssue$Freq/sum(socialIssue$Freq), digits = 1)
    socialIssue$label = paste(socialIssue$Var1," (", socialIssue$percent,"%)", sep = "")
    pie(socialIssue$Freq, labels = socialIssue$label, col = cols)
    

    【讨论】:

    • 非常感谢
    【解决方案2】:

    这样做:

    pie(socialIssue[, 1],
        labels = paste0(round(socialIssue[, 1] / sum(socialIssue[, 1]) * 100, 2), "%"))
    

    因为你有一个矩阵,而不是一个数据框。

    【讨论】:

    • 谢谢...但是有什么办法可以写下图表的标题并在底部显示图例。图例应说明类别,百分比应显示在饼图上。
    【解决方案3】:

    prop.table 负责 % 计算 - sprintf 处理数字值的格式,因此您有一致的小数位。

    也不需要所有的转换代码:

    socialIssue <- matrix(c(245,112,153,71,133,306),ncol=1,byrow=T)
    pie(socialIssue, labels=sprintf("%.2f%%", prop.table(socialIssue)*100))
    

    【讨论】:

      【解决方案4】:

      使用 base R,使用您使用的颜色(参数名称应为 cols 而不是 `colnames'),并添加图例:

      pie(socialIssue[,1], labels=paste0(round(socialIssue/sum(socialIssue)*100,2),"%"),col=cols)
      legend('bottomright', legend=rownames(socialIssue), fill=cols, bty='n')
      

      ggplot2

      socialIssue <- matrix(c(245,112,153,71,133,306),ncol=1,byrow=T)
      rownames(socialIssue) <- c("Housing","Transportation","Health Care","Education","Food","Other")
      colnames(socialIssue) <- c("Frequency")
      library(ggplot2)
      library(scales)
      ggplot(as.data.frame(socialIssue), aes(x='',y=Frequency, fill=as.factor(Frequency))) + 
        geom_bar(width=1, stat='identity')  +   
        scale_fill_manual(values=cols, labels=rownames(socialIssue)) +
        scale_y_continuous(labels=percent) + 
        coord_polar(theta = "y") + theme_bw()
      

      【讨论】:

      • 非常感谢
      猜你喜欢
      • 2015-06-11
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2011-05-03
      • 2013-06-29
      • 1970-01-01
      • 2012-10-18
      相关资源
      最近更新 更多