显示饼图百分比在从数据集ř工作室

问题描述 投票:0回答:1

我想,以显示不同的值出现的百分比使用R.在我的数据集的每一列

这个简单的代码行创建从指定的列的表,并显示具有正确分布的完美饼图。不过,我似乎无法显示的百分比值。

我相信这是一个短期和简单的方法来做到这一点。

这是我到目前为止所。请我需要什么补充?

> workclass <- table(adult$workclass)
> pie(workclass) 

谢谢。

这些人是我“workclass”一栏,我有一个饼图的值。我只需要在饼图上显示它们的分布百分比。

Federal-gov - 1836 Occurrences     
Local-gov - 960 Occurrences
Never-worked - 2093 Occurrences
Private - 120 Occurrences
Self-emp-inc - 2541 Occurrences
Self-emp-not-inc - 1116 Occurrences
State-gov - 2093 Occurrences
Without-pay - 1298 Occurrences
r dataset pie-chart data-analysis percentage
1个回答
1
投票

我希望这个代码对你的作品,饼图中显示的百分比。

adult <- data.frame(workclass = c(rep("Federal-gov",1836),rep("Local-gov",960),rep("Never-worked",2093),
                                  rep("Private",120), rep("Self-emp-inc",2541), rep("Self-emp-not-inc",1116),
                                  rep("State-gov",2093),rep("Without-pay",1298))) 
           Occurrences = c(1836,960,2093,120,2541,1116,2093,1298))

workclass <- table(adult$workclass)


par(mar = c(2,2,2,2))
lb = paste0(round(prop.table(workclass)*100,2),"%")
pie(workclass,labels = lb, col = rainbow(8))
legend(-2.1,0.4,legend=names(workclass),cex=0.7,yjust=0.2, xjust = -0.1,
       fill = rainbow(8), bty = "n")

enter image description here

prop.table(workclass)
Federal-gov        Local-gov     Never-worked          Private     Self-emp-inc Self-emp-not-inc 
     0.152276686      0.079621796      0.173592104      0.009952725      0.210748943      0.092560338 
       State-gov      Without-pay 
     0.173592104      0.107655304 
© www.soinside.com 2019 - 2024. All rights reserved.