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)


問題描述

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 Exchhattuthelatemail)

參考文件

  1. Make bins of each table row and draw stack bar figure in R (CC BY‑SA 3.0/4.0)

#R






相關問題

如何將均值、標準差等函數應用於整個矩陣 (How to apply mean, sd etc. function to a whole matrix)

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)

Reading not quite correct .csv file in R (Reading not quite correct .csv file in R)

包'treemap'中的線條粗細 (Thickness of lines in Package ‘treemap’)

是否需要帶有 awk 的預處理文件,或者可以直接在 R 中完成? (Is preprocessing file with awk needed or it can be done directly in R?)

rpivotTable 選擇元素下拉菜單 (rpivotTable select elements drop down menu)

優化性能 - Shiny 中的大文件輸入 (Optimizing Performance - Large File Input in Shiny)

數值取決於所應用的應用系列,R (Numeric values depending of apply family applied, R)

如何記錄全年的值? (How to note the values across year?)

R中的線性搜索 (Linear search in R)

在 dplyr/purrr 工作流程中動態連接多個數據集 (Dynamically join multiple datasets in a dplyr/purrr workflow)

如何將行值更改為列名 (R) (How change Row values to Column names (R))







留言討論