Create several pie charts in R from lists

廉价感情. 提交于 2019-12-14 03:56:28

问题


I want to create several pie charts at once, I have a list of the names:

 [1]   361   456   745   858  1294  1297  2360  2872  3034  5118  5189...

So the first pie chart should be labeled '361', and so on.

Then I have several lists with values for each pie chart

[1] 102  99 107  30   2   8  24  16  57 117 ...
[1] 1 1 2 1 0 0 0 1 1 2 ...
[1] 4 2 2 1 3 0 0 1 1 2 ...

So for '361', the first element is 102, the second is 1 and the third is 4. The total is 107.

I want to do all of the charts at once.


回答1:


One way to get that is by setting par("mfrow"). I also adjusted the margins a bit to eliminate some unwanted whitespace around the charts.

par(mfrow=c(2,5), mar=rep(0, 4), oma=rep(0,4))
for(i in 1:length(names)) {
    pie(df[i, ][df[i,] > 0], labels=(1:3)[df[i,] > 0])
    title(names[i], line = -3) }

Data

## data
names = c(361, 456, 745, 858, 1294, 1297, 2360, 2872, 3034, 5118, 5189)
x = c(102,  99, 107,  30,   2,   8,  24,  16,  57, 117)
y = c(1, 1, 2, 1, 0, 0, 0, 1, 1, 2)
z = c(4, 2, 2, 1, 3, 0, 0, 1, 1, 2)
df = data.frame(x,y,z)


来源:https://stackoverflow.com/questions/46286485/create-several-pie-charts-in-r-from-lists

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