从列表在R中创建多个饼图 [英] Create several pie charts in R from lists
本文介绍了从列表在R中创建多个饼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想一次创建多个饼图,我有一个名称列表:
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...
因此,第一个饼图应标记为"361",依此类推.
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 ...
因此对于"361",第一个元素为102,第二个为1,第三个为4.总计为107.
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.
推荐答案
一种方法是通过设置par("mfrow")
.我还对边距进行了一些调整,以消除图表周围不必要的空白.
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
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)
这篇关于从列表在R中创建多个饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文