[英]Print frequencies (as numbers) in plot
在R中,我想在图表中插入频率(作为数字):
my code to create the plot:
par(mar=c(4.5,4.5,9.5,4), xpd=TRUE)
plot(factor(ArtMehrspr)~Mehrspr_Vielf, data=datProjektMehr, col=terrain.colors(4),
bty='L', main="Vielfalt nutzen")
legend("topright", inset=c(0,-.225), title="Art der Mehrsprachigkeit", levels(factor(datProjektMehr$ArtMehrspr)),
fill=terrain.colors(4), horiz=TRUE)
par(mar=c(5,4,4,2)+0.1)
在该图中,描绘了我的数据ArtMehrspr
两列: ArtMehrspr
和Mehrspr_Vielf
。 现在我想知道的是,类别“ 1”中有多少“ Kombi”,类别“ 1”中有多少“ Paral”,依此类推,然后在绘图中打印此数字,以便在每个在图的方框中,我可以看到相应的观测值数量。 R必须知道这些数字,否则R不能根据观察次数改变不同盒子的高度。 因此,将这些数字输入情节并不难,不是吗?
使用命令table()
,我可以获得这些数字,但是我必须有5个table()
命令才能获得所有数字。 类别= 1的示例:
> table(subset(datProjektMehr, Mehrspr_Vielf=="1")$ArtMehrspr)
einspr Kombi Paral Versc Wechs
0 1 9 2 1
显然,您可以通过添加命令labels = TRUE
。 但这不起作用:
par(mar=c(4.5,4.5,9.5,4), xpd=TRUE, labels = TRUE)
plot(factor(ArtMehrspr)~Mehrspr_Vielf, data=datProjektMehr, col=terrain.colors(4),
bty='L', main="Vielfalt nutzen")
legend("topright", inset=c(0,-.225), title="Art der Mehrsprachigkeit", levels(factor(datProjektMehr$ArtMehrspr)),
fill=terrain.colors(4), horiz=TRUE)
par(mar=c(5,4,4,2)+0.1)
R给我以下警告消息:
Warning message:
In par(mar = c(4.5, 4.5, 9.5, 4), xpd = TRUE, labels = TRUE) :
"labels" is not a graphical parameter
这不是正确的命令吗? 有谁知道如何做到这一点?
首先, warning
您在par
内没有可以使用的labels
参数。
关于table
输出的绘制,我不知道是否有一种简单的方法来执行此操作,但是我管理了一个相当不可靠且效率低下的代码。 但是,在我的机器上,它每次运行都可以运行。
我想到的概念是在table
内将table
所有值发送text
。 为此,必须估算xx'和yy'中的坐标。 我更喜欢术语“估计的”而不是“计算的”,因为由于plot
方法是plot.factor
的事实,我没有找到一种计算坐标绝对值的方法。
所以:
#random data. DF = datProjektMehr, artmehr = ArtMehrspr, mehrviel = Mehrspr_Vielf
DF <- data.frame(artmehr = sample(letters[1:4], 20, T), mehrviel = as.factor(sample(1:5, 20, T)))
#your code of plotting
par(mar = c(4.5,4.5,9.5,4), xpd = TRUE)
plot(factor(artmehr) ~ mehrviel, data = DF, col = terrain.colors(4),
bty = 'L', main = "Vielfalt nutzen")
legend("topright", inset=c(0,-.225), title="Art der Mehrsprachigkeit", levels(factor(DF$artmehr)),
fill=terrain.colors(4), horiz=TRUE)
#no need to "table()" many times
tab = table(DF$artmehr, DF$mehrviel)
#maximum value of x axis (at least in my machine)
#I found -through trial and error- that for a factor of n levels, x.max = 1 + (n-1)*0.02
x.max = 1 + (length(levels(DF$mehrviel)) - 1) * 0.02
#coordinates of "mehrviel" (as I named it)
mehrviel.coords = ((cumsum(apply(tab, 2, sum)) / sum(tab)) * x.max) - ((apply(tab, 2, sum) / sum(tab)) / 2)
#coordinates of "artmehr" (as I named it)
artmehr.coords <- apply(tab, 2, function(x) { cumsum(x / sum(x)) })
artmehr.coords <- apply(artmehr.coords, 2, function(x) { x - c(x[1]/2, diff(x)/2) })
#"text" the values in your table
#don't plot "0"s
for(i in 1:ncol(artmehr.coords))
{
text(x = mehrviel.coords[i], y = artmehr.coords[,i], labels = ifelse(tab[,i] != 0, tab[,i], ""), cex = 2)
}
table
的值:
tab
1 2 3 4 5
a 1 1 0 1 0
b 0 0 2 1 2
c 1 1 2 1 0
d 2 0 0 3 2
剧情:
编辑:1)“整理”答案。 2)在xx'轴上绘制的因子上增加了一个额外的level
以完全匹配您的数据。 3) text
编频率中的每个框的中间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.