I would like to make a series of plots using ggplot from multiple different dataframes. I was planning on using a list and iterating over the list as follows:
libraries <- objects() #make a list of the dataframes we want to graph
for(i in libraries) {
# create initial plots
x1 <- qplot(data= i, V1, reorder(V2,V3), color = V3) + coord_flip()
x2 <- ggplot(i, aes(x=reorder(V2,V3), group=V3, color=V3)) + geom_bar()
x3 <- ggplot(i, aes(x=V1, group=V3, color=V3)) + coord_flip() + geom_bar()
}
however I get the error message:
Error: ggplot2 doesn't know how to deal with data of class factor
presumably because ‘libraries’ is now a character variable and not a data frame. Any one have another suggestion on how to iterate through the dataframes? I suppose I could merge them with plyr and then ggplot a subset of the data but that seems to add more work.
The usual way to iterate over data.frames (which are just regularly organized lists) is with
lapply:In the case of producing a list of ggplot-objects I would imagine that the Hadley-method would instead be to use
llply, but I’m not a skilled plyr-user, so let me suggest this totally untested code template: