1

我正计划制作堆栈条形图,就像 在 R

我有没有任何标题的表格格式的原始数据(每行包含七个值):

1.028 1.125 1.475 0.793 0.803 0.815 0.974
0.867 1.115 1.256 1.377 1.029 1.184 1.135
0.497 0.400 0.313 0.570 0.319 0.558 0.475
0.541 0.646 0.309 0.692 0.813 0.575 0.806
1.153 1.184 0.792 0.666 0.976 0.607 0.706
1.236 1.049 1.424 1.773 1.019 0.910 1.376

我的计划是首先为每行制作步长为 1 的不同间隔的箱。

0-1  1-2  2-3
4    3    0
1    6    0
...

其次,在 x 轴上制作每行间隔的堆叠条。

我做了很多谷歌搜索。我停在了这里......

> mytable = read.table("./temp.out")
> m <-apply(mytable, 1, cut, seq(0, 3, 1))
> m
[,1]    [,2]    [,3]    [,4]    [,5]    [,6]    [,7]
[1,] "(1,2]" "(0,1]" "(0,1]" "(0,1]" "(0,1]" "(1,2]" "(1,2]"

[2,] "(1,2]" "(1,2]" "(0,1]" "(0,1]" "(0,1]" "(1,2]" "(1,2]"

[3,] "(1,2]" "(1,2]" "(0,1]" "(0,1]" "(0,1]" "(0,1]" "(1,2]"

[4,] "(0,1]" "(1,2]" "(0,1]" "(0,1]" "(0,1]" "(0,1]" "(1,2]"

[5,] "(0,1]" "(1,2]" "(0,1]" "(0,1]" "(0,1]" "(0,1]" "(1,2]"

[6,] "(0,1]" "(1,2]" "(0,1]" "(0,1]" "(0,1]" "(0,1]" "(0,1]"

[7,] "(0,1]" "(1,2]" "(0,1]" "(0,1]" "(0,1]" "(0,1]" "(1,2]"

之后,我无法在频率表中将其转换为

0-1  1-2  2-3
4    3    0
1    6    0
...

你能建议我如何制作频率表吗?得到频率表后,这些数据可以绘制在堆栈栏中吗?

4

1 回答 1

0

您可以使用匿名函数获取所需的信息,如下所示:

m <- t(apply(mytable, 1, function(x) table(cut(x,seq(0, 3, 1)))))

> m
     (0,1] (1,2] (2,3]
[1,]     4     3     0
[2,]     1     6     0
[3,]     7     0     0
[4,]     7     0     0
[5,]     5     2     0
[6,]     1     6     0

如果您将数据保留为转置格式而没有t类似...

m <- apply(mytable, 1, function(x) table(cut(x,seq(0, 3, 1))))

...您可以按原样绘制条形图:

barplot(m)
于 2013-03-28T01:39:25.327 回答