从rle()类对象列表中绘制多个直方图

Joe*_*eri 3 plot r

我有为数据集中的每个单独的ID创建的rle()类对象,现在我想将它们绘制在单独的直方图中,这些直方图显示各种长度类的频率,以便获得它们的分布图,但我可以'似乎弄清楚如何做到这一点.

我通过使用以下代码对具有各种ID的数据运行rle()函数来获取rle()类对象的列表:

list.runs<-dlply(data.1, .(ID), function(x) rle(x$flights))
Run Code Online (Sandbox Code Playgroud)

但是这使得无法将数据传输到数据帧中,因为rle()对象无法强制转换为数据帧.所以我把它们解开了:

list.runs<-dlply(data.1, .(ID), function(x) unclass(rle(x$flights)))
Run Code Online (Sandbox Code Playgroud)

但我无法将这些数据放在数据框中,因为列表的长度不同.

runs<-ldply(do.call(data.frame,list.runs))

Error in function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
arguments imply differing number of rows: 14, 13
Run Code Online (Sandbox Code Playgroud)

问题:如何绘制每个单独ID的长度值的直方图?

数据(简化):

> dput(data.1)
structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), flights = c(1, 1, 1, 
1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 
0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 
1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 
1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 
1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1)), .Names = c("ID", "flights"
), row.names = c(NA, -100L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

Aru*_*run 6

我不知道你要做什么,但我会在这里展示如何:

require(plyr)
list.runs <- ddply(data.1, .(ID), function(x) {
    rr <- rle(x$flights)
    data.frame(freq=rr$lengths, xvar=seq_along(rr$lengths))
})

require(ggplot2)
ggplot(data = list.runs, aes(x = factor(xvar), y = freq)) + 
        geom_bar(stat = "identity", aes(fill=factor(ID))) + 
          facet_wrap( ~ ID, ncol=2)
Run Code Online (Sandbox Code Playgroud)

给你:

在此输入图像描述

编辑:遵循OP的评论:您也可以直接从这些数据中获取.实际上,您不必为您的要求生成"xvar".来自list.runs:

ggplot(data = list.runs, aes(x = factor(freq))) + 
     geom_bar(aes(weights = ..count.., fill=factor(ID))) + 
     facet_wrap( ~ ID, ncol=2)
Run Code Online (Sandbox Code Playgroud)

得到:

在此输入图像描述