I have rle() class objects that are created for each separate ID in a dataset, and now I want to plot them in separate histograms that display the frequency of various length classes in order to get a picture of their distribution, but I can't seem to figure out how to do this.
I obtained a list of rle() class objects by running the rle() function over data with various IDs, using the following code:
list.runs<-dlply(data.1, .(ID), function(x) rle(x$flights))
But this made it impossible to transfer the data into a dataframe because the rle() objects could not be coerced into a dataframe. Therefore I unclassed them:
list.runs<-dlply(data.1, .(ID), function(x) unclass(rle(x$flights)))
But I can't put this data in a dataframe because the lists are of different lenghts.
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
The question: How can I plot the histograms of the length values for each separate ID?
The data (simplified):
> 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")
I don't know what you're trying to do, but I'll show here just how:
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)
Gives you:

Edit: following OP's comment: You can get that directly from this data as well. In fact, you don't have to generate "xvar" for your requirements. From list.runs:
ggplot(data = list.runs, aes(x = factor(freq))) +
geom_bar(aes(weights = ..count.., fill=factor(ID))) +
facet_wrap( ~ ID, ncol=2)
gives:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With