简体   繁体   English

在图表中打印频率(数字)

[英]Print frequencies (as numbers) in plot

In R, I would like to insert frequencies (as numbers) in a 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) 

在此处输入图片说明

In the plot, 2 columns of my dataframe are depicted: ArtMehrspr and Mehrspr_Vielf . 在该图中,描绘了我的数据ArtMehrspr两列: ArtMehrsprMehrspr_Vielf Now what I would like to know is, how many "Kombi" are in category "1", how many "Paral" are in category "1" and so on, and then to print this number in the plot, so that in every box of the plot, I can see the corresponding number of observations. 现在我想知道的是,类别“ 1”中有多少“ Kombi”,类别“ 1”中有多少“ Paral”,依此类推,然后在绘图中打印此数字,以便在每个在图的方框中,我可以看到相应的观测值数量。 R must know these numbers, otherwise it could not vary the height of the different boxes according to the number of observations. R必须知道这些数字,否则R不能根据观察次数改变不同盒子的高度。 So it cannot be that hard to get these numbers into the plot, can it? 因此,将这些数字输入情节并不难,不是吗?

With the command table() , I can get these numbers, but I would have to have 5 table() -commands to get all the numbers. 使用命令table() ,我可以获得这些数字,但是我必须有5个table()命令才能获得所有数字。 Example for category = 1: 类别= 1的示例:

> table(subset(datProjektMehr, Mehrspr_Vielf=="1")$ArtMehrspr)  
einspr  Kombi  Paral  Versc  Wechs   
0          1       9     2       1 

Apparently, you can achieve what I am looking for by adding the command labels = TRUE . 显然,您可以通过添加命令labels = TRUE But it does not work: 但这不起作用:

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 gives me the following warning message: R给我以下警告消息:

Warning message:
In par(mar = c(4.5, 4.5, 9.5, 4), xpd = TRUE, labels = TRUE) :
  "labels" is not a graphical parameter

Is this not the right command? 这不是正确的命令吗? Does anyone know how to do this? 有谁知道如何做到这一点?

First of all, the warning informs that there is not a labels argument you can use inside par . 首先, warning您在par内没有可以使用的labels参数。

Regarding the plotting of the table output, I'm not aware if there is an easy way of doing this, but I managed a pretty UNreliable and, maybe, inefficient code. 关于table输出的绘制,我不知道是否有一种简单的方法来执行此操作,但是我管理了一个相当不可靠且效率低下的代码。 In my machine, though, it works every time I run it. 但是,在我的机器上,它每次运行都可以运行。

The concept I had in mind is to text all values from your table inside the plot. 我想到的概念是在table内将table所有值发送text To do so, coordinates in xx' and yy' had to be estimated. 为此,必须估算xx'和yy'中的坐标。 I prefer the term "estimated" instead of "calculated" because I didn't find a way to compute absolute values for the coordinates, due to the fact that the plot method was plot.factor . 我更喜欢术语“估计的”而不是“计算的”,因为由于plot方法是plot.factor的事实,我没有找到一种计算坐标绝对值的方法。

So: 所以:

    #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)
     }

The values of table : 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

The plot: 剧情:

plot_example

EDIT: 1) "Tidied" the answer. 编辑:1)“整理”答案。 2) Aadded an extra level to the factor ploted in xx' axis to match your data exactly. 2)在xx'轴上绘制的因子上增加了一个额外的level以完全匹配您的数据。 3) text ed the frequencies in the middle of each box. 3) text编频率中的每个框的中间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM