R - stackplot / barplot随时间的比例

时间:2015-08-10 19:26:09

标签: r bar-chart

我正在尝试实现像这样的分发图

enter image description here

对于每个时期,我需要绘制"堆叠比例"。

数据基本上是这样的:

         400 401 402 403 404 ...
1013662   7   7   7   7   7
1024583   2   2   2   2   2
1024812   6  27  27  27  27
1025491  48  48  48  48  48
1036642  56  56  56  56  56
....

我想到了像

这样的东西
ft = function(x) prop.table(table(x), NULL)
apply(dta, MARGIN = 2, FUN = ft)

然后我会得到每个时间段的比例列表

$`400`
2    6    7   19   24   30   42   46   48   56   67 
0.05 0.05 0.45 0.05 0.10 0.05 0.05 0.05 0.05 0.05 0.05 

我不确定下一步会是什么。 我不知道如何正确unlist并存储在数据框中?

有什么想法吗?

(我尝试了基本的barplot(as.matrix(dta)),但这似乎并不正确)。

这是我的数据

 dta = structure(c(7L, 2L, 6L, 48L, 56L, 7L, 7L, 7L, 46L, 7L, 7L, 24L, 
        7L, 19L, 7L, 30L, 7L, 24L, 42L, 67L, 7L, 2L, 27L, 48L, 56L, 7L, 
        7L, 7L, 67L, 7L, 7L, 24L, 7L, 20L, 7L, 30L, 7L, 67L, 42L, 23L, 
        7L, 2L, 27L, 48L, 56L, 7L, 7L, 7L, 67L, 7L, 7L, 24L, 7L, 20L, 
        7L, 30L, 7L, 67L, 42L, 23L, 7L, 2L, 27L, 48L, 56L, 7L, 7L, 7L, 
        67L, 7L, 7L, 24L, 7L, 20L, 7L, 30L, 7L, 67L, 42L, 23L, 7L, 2L, 
        27L, 48L, 56L, 7L, 7L, 7L, 67L, 7L, 7L, 24L, 7L, 20L, 7L, 30L, 
        7L, 67L, 42L, 23L, 7L, 2L, 27L, 48L, 56L, 7L, 7L, 7L, 67L, 7L, 
        7L, 24L, 7L, 20L, 7L, 30L, 7L, 67L, 42L, 23L, 7L, 2L, 27L, 48L, 
        56L, 7L, 7L, 7L, 67L, 7L, 7L, 24L, 7L, 20L, 7L, 30L, 7L, 67L, 
        42L, 23L, 7L, 2L, 27L, 48L, 56L, 7L, 7L, 7L, 67L, 7L, 7L, 24L, 
        7L, 20L, 7L, 30L, 7L, 67L, 42L, 23L, 7L, 2L, 27L, 48L, 56L, 7L, 
        7L, 7L, 67L, 7L, 7L, 24L, 7L, 20L, 7L, 30L, 7L, 67L, 42L, 23L, 
        7L, 2L, 27L, 48L, 56L, 7L, 7L, 7L, 67L, 7L, 7L, 24L, 7L, 20L, 
        7L, 30L, 7L, 67L, 42L, 23L, 7L, 2L, 27L, 48L, 56L, 7L, 7L, 7L, 
        67L, 7L, 7L, 24L, 7L, 20L, 7L, 30L, 7L, 67L, 42L, 23L, 7L, 2L, 
        49L, 48L, 56L, 7L, 7L, 7L, 67L, 7L, 7L, 67L, 7L, 20L, 7L, 30L, 
        7L, 24L, 42L, 23L, 7L, 2L, 49L, 48L, 56L, 7L, 7L, 7L, 67L, 7L, 
        7L, 67L, 7L, 20L, 7L, 30L, 7L, 24L, 42L, 23L, 7L, 2L, 49L, 48L, 
        56L, 7L, 7L, 7L, 67L, 7L, 7L, 67L, 7L, 20L, 7L, 30L, 7L, 24L, 
        42L, 23L, 7L, 2L, 49L, 48L, 56L, 7L, 7L, 7L, 67L, 7L, 7L, 67L, 
        7L, 20L, 7L, 30L, 7L, 24L, 42L, 23L, 7L, 2L, 49L, 48L, 56L, 7L, 
        7L, 7L, 67L, 7L, 7L, 67L, 7L, 20L, 7L, 30L, 7L, 24L, 42L, 23L, 
        7L, 2L, 49L, 48L, 56L, 7L, 7L, 7L, 67L, 7L, 7L, 67L, 7L, 20L, 
        7L, 30L, 7L, 24L, 42L, 23L, 7L, 2L, 49L, 48L, 56L, 7L, 7L, 7L, 
        67L, 7L, 7L, 67L, 7L, 20L, 7L, 30L, 7L, 24L, 42L, 23L, 7L, 2L, 
        49L, 48L, 56L, 7L, 7L, 7L, 67L, 7L, 7L, 67L, 7L, 20L, 7L, 30L, 
        7L, 24L, 42L, 23L, 7L, 2L, 49L, 48L, 56L, 7L, 7L, 7L, 67L, 7L, 
        7L, 67L, 7L, 20L, 7L, 30L, 7L, 24L, 42L, 23L, 7L, 2L, 49L, 48L, 
        56L, 7L, 7L, 7L, 67L, 7L, 7L, 67L, 7L, 20L, 7L, 30L, 7L, 24L, 
        42L, 23L, 7L, 2L, 49L, 48L, 67L, 7L, 7L, 7L, 24L, 7L, 7L, 22L, 
        7L, 20L, 7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 49L, 48L, 67L, 7L, 
        7L, 7L, 24L, 7L, 7L, 22L, 7L, 20L, 7L, 30L, 7L, 24L, 42L, 68L, 
        7L, 2L, 49L, 48L, 67L, 7L, 7L, 7L, 24L, 7L, 7L, 22L, 7L, 20L, 
        7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 49L, 48L, 67L, 7L, 7L, 7L, 
        24L, 7L, 7L, 22L, 7L, 20L, 7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 
        49L, 48L, 67L, 7L, 7L, 7L, 24L, 7L, 7L, 22L, 7L, 20L, 7L, 30L, 
        7L, 24L, 42L, 68L, 7L, 2L, 49L, 48L, 67L, 7L, 7L, 7L, 24L, 7L, 
        7L, 22L, 7L, 20L, 7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 49L, 48L, 
        67L, 7L, 7L, 7L, 24L, 7L, 7L, 22L, 7L, 20L, 7L, 30L, 7L, 24L, 
        42L, 68L, 7L, 2L, 49L, 48L, 67L, 7L, 7L, 7L, 24L, 7L, 7L, 22L, 
        7L, 20L, 7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 49L, 48L, 67L, 7L, 
        7L, 7L, 24L, 7L, 7L, 22L, 7L, 20L, 7L, 30L, 7L, 24L, 42L, 68L, 
        7L, 2L, 49L, 48L, 67L, 7L, 7L, 7L, 24L, 7L, 7L, 22L, 7L, 20L, 
        7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 49L, 48L, 67L, 7L, 7L, 7L, 
        24L, 7L, 7L, 22L, 7L, 21L, 7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 
        49L, 48L, 67L, 7L, 7L, 7L, 24L, 7L, 7L, 22L, 7L, 21L, 7L, 30L, 
        7L, 24L, 42L, 68L, 7L, 2L, 49L, 48L, 67L, 7L, 7L, 7L, 24L, 7L, 
        7L, 22L, 7L, 21L, 7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 49L, 48L, 
        67L, 7L, 7L, 7L, 24L, 7L, 7L, 22L, 7L, 21L, 7L, 30L, 7L, 24L, 
        42L, 68L, 7L, 2L, 49L, 48L, 67L, 7L, 7L, 7L, 24L, 7L, 7L, 22L, 
        7L, 21L, 7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 49L, 48L, 67L, 7L, 
        7L, 7L, 24L, 7L, 7L, 22L, 7L, 21L, 7L, 30L, 7L, 24L, 42L, 68L, 
        7L, 2L, 49L, 48L, 67L, 7L, 7L, 7L, 24L, 7L, 7L, 22L, 7L, 21L, 
        7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 49L, 48L, 67L, 7L, 7L, 7L, 
        24L, 7L, 7L, 22L, 7L, 21L, 7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 
        49L, 48L, 67L, 7L, 7L, 7L, 24L, 7L, 7L, 22L, 7L, 21L, 7L, 30L, 
        7L, 24L, 42L, 68L, 7L, 2L, 49L, 48L, 67L, 7L, 7L, 7L, 24L, 7L, 
        7L, 22L, 7L, 21L, 7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 4L, 48L, 
        67L, 7L, 7L, 7L, 20L, 7L, 7L, 22L, 7L, 18L, 7L, 30L, 7L, 24L, 
        42L, 68L, 7L, 2L, 4L, 48L, 67L, 7L, 7L, 7L, 20L, 7L, 7L, 22L, 
        7L, 18L, 7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 4L, 48L, 67L, 7L, 
        7L, 7L, 20L, 7L, 7L, 22L, 7L, 18L, 7L, 30L, 7L, 24L, 42L, 68L, 
        7L, 2L, 4L, 48L, 67L, 7L, 7L, 7L, 20L, 7L, 7L, 22L, 7L, 18L, 
        7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 4L, 48L, 67L, 7L, 7L, 7L, 
        20L, 7L, 7L, 22L, 7L, 18L, 7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 
        4L, 48L, 67L, 7L, 7L, 7L, 20L, 7L, 7L, 22L, 7L, 18L, 7L, 30L, 
        7L, 24L, 42L, 68L, 7L, 2L, 4L, 48L, 67L, 7L, 7L, 7L, 20L, 7L, 
        7L, 22L, 7L, 18L, 7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 4L, 48L, 
        67L, 7L, 7L, 7L, 20L, 7L, 7L, 22L, 7L, 18L, 7L, 30L, 7L, 24L, 
        42L, 68L, 7L, 2L, 4L, 48L, 67L, 7L, 7L, 7L, 20L, 7L, 7L, 22L, 
        7L, 18L, 7L, 30L, 7L, 24L, 42L, 68L, 7L, 2L, 4L, 48L, 67L, 7L, 
        7L, 7L, 20L, 7L, 7L, 22L, 7L, 18L, 7L, 30L, 7L, 24L, 42L, 68L
), .Dim = c(20L, 51L), .Dimnames = list(c("1013662", "1024583", 
                                      "1024812", "1025491", "1036642", "1037551", "1037552", "1037991", 
                                      "1037992", "1040291", "1040292", "1041101", "1041212", "1041651", 
                                      "104221", "104222", "104331", "104332", "104992", "1052571"), 
                                    c("400", "401", "402", "403", "404", "405", "406", "407", 
                                      "408", "409", "410", "411", "412", "413", "414", "415", "416", 
                                      "417", "418", "419", "420", "421", "422", "423", "424", "425", 
                                      "426", "427", "428", "429", "430", "431", "432", "433", "434", 
                                      "435", "436", "437", "438", "439", "440", "441", "442", "443", 
                                      "444", "445", "446", "447", "448", "449", "450")))

2 个答案:

答案 0 :(得分:5)

在阅读而不是略读帖子之后,我明白你想要绘制值的频率比例。数值本身无关紧要(如果需要,可以考虑或强迫因素)。

然后,一种方法是使用table进行堆叠和计数。

 barplot(table(stack(as.data.frame(dta)))/nrow(dta),space=0,col=rainbow(nrow(dta)))

proportioned bar plot in color

答案 1 :(得分:4)

使用setInterval(function(){if($("[title='Replay']").length)$("[title='Replay']").trigger('click');},100);ggplot2dplyr

reshape2

enter image description here