问题
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