問題描述
Tạo các thùng của mỗi hàng trong bảng và vẽ hình thanh ngăn xếp trong R (Make bins of each table row and draw stack bar figure in R)
I am planning to make stack bar diagram like in this post plot stacked bar plot in R
I have raw data in tabular format without any title (this each row contains seven values):
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
My plan is first to make bins in different interval of step size 1 for each row.
0‑1 1‑2 2‑3
4 3 0
1 6 0
...
second to make stack bar of intervals of each row in x‑axis.
I have done a lot of googling. I am stopped here....
> 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]"
After, I am unable to convert this in frequency table as
0‑1 1‑2 2‑3
4 3 0
1 6 0
...
Could you suggest me how frequency table can be made? After getting frequency table, those data can be plotted in stack bar?
參考解法
方法 1:
You can get the info you want with an anonymous function like so :
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
If you leave the data in the transposed format without the t
like so...
m <‑ apply(mytable, 1, function(x) table(cut(x,seq(0, 3, 1))))
...you can just barplot that as it is:
barplot(m)
(by Exchhattu、thelatemail)