问题
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.
回答1:
The usual way to iterate over data.frames (which are just regularly organized lists) is with lapply:
df1 <- data.frame(date = as.Date(10*365*rbeta(100, .5, .1)),group="a")
df2 <- data.frame(date = as.Date(10*365*rbeta(50, .1, .5)),group="b")
df3 <- data.frame(date = as.Date(10*365*rbeta(25, 3,3)),group="c")
dfrmL <- list(df1,df2,df3)
lapply(dfrmL, NROW)
[[1]]
[1] 100
[[2]]
[1] 50
[[3]]
[1] 25
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:
plts <- lapply(dfrmL, function(df) qplot(qplot(data= df,
V1, reorder(V2,V3), color = V3) +
coord_flip()
)
# you may need to explicitly print() or plot() the plots as stated in the R-FAQ.
lapply(plts, print)
回答2:
A more complete, reproducible example may help us suggest a better way to accomplish this, but at the very least I can suggest replacing:
libraries <- objects()
with this
libraries <- lapply(objects(), FUN = get)
which will actually build a list of all the objects in the current environment. But I somehow doubt that the data frames are the only objects in your environment, so perhaps you would rather grab the list of objects using objects or ls, use grep (or a related function) to find only your data frames based on their names and then get just those data frames using lapply.
Finally, you can then iterate over them as @Dwin describes.
来源:https://stackoverflow.com/questions/7787902/loop-over-dataframes-in-ggplot2