How to plot pie chart in R from a table with relative Frequency?

徘徊边缘 提交于 2019-12-20 06:36:33

问题


I am brand new to R. I need to plot a pie graph. Now I have tried my best but it's not generating a pie chart for me. Below is my code.

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)

This is the following output. The frequency outputted is correct.

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

回答1:


Convert to dataframe and then plot

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:


This does it:

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

Because you have a matrix, not a data frame.




回答3:


prop.table takes care of the % calculation - sprintf deals with the formatting of the number values so you have consistent decimal places.

All of the conversion code isn't required either:

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



回答4:


With base R, with the colors you used (the param name should be cols instead of `colnames'), with legends added:

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

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



来源:https://stackoverflow.com/questions/41775531/error-in-pie-x-values-must-be-positive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!