我有为数据集中的每个单独ID创建的rle()类对象,现在我想在单独的直方图中绘制它们,这些直方图显示各种长度类的频率,以便获得它们的分布图,但是我似乎无法弄清楚如何做到这一点。
通过使用以下代码对具有各种ID的数据运行rle()函数,我获得了rle()类对象的列表:
list.runs<-dlply(data.1, .(ID), function(x) rle(x$flights))
但这使得无法将数据传输到数据帧中,因为无法将rle()对象强制转换为数据帧。所以我把它们解开了:
list.runs<-dlply(data.1, .(ID), function(x) unclass(rle(x$flights)))
但是我无法将这些数据放在数据框中,因为列表的长度不同。
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
问题:如何为每个单独的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")
答案 0 :(得分: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)
给你:
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)
给出:
答案 1 :(得分:1)
我认为@ Arun在ddply
调用中直接访问data.frame的方法是可行的方法,但只是为了说明如何从list.runs
对象转到一个有用的data.frame:
df.summary <- ldply(list.runs,function(x,...) do.call(data.frame,x))
library(ggplot2)
ggplot(df.summary, aes(factor(lengths),values)) +
geom_bar(stat = "identity", aes(fill=factor(ID))) +
facet_grid( ~ ID, ncol=2)