Since you want to split the data set and plot for each factor level, I would apply it to one of the split-apply-return tools from the plyr package.
Here is an example toy using the mtcars . First create a graph and name it p , then use dlply to split the data set by a coefficient and return a graph for each level. I use %+% of ggplot2 to replace data.frame in the plot.
p = ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_line() require(plyr) dlply(mtcars, .(cyl), function(x) p %+% x)
This returns all charts, one by one. If you name the resulting list object, you can also call one graph at a time.
plots = dlply(mtcars, .(cyl), function(x) p %+% x) plots[1]
Edit
I started thinking about putting a headline on each plot based on a factor that seemed to be useful.
dlply(mtcars, .(cyl), function(x) p %+% x + facet_wrap(~cyl))
Edit 2
Here is one way to save them in one document, one chart per page. This works with a list of graphs called plots . He saves them all in one document, one chart per page. I have not changed the default values ββin pdf , but you can, of course, examine the changes you can make.
pdf() plots dev.off()
Updated to use dplyr instead of plyr . This is done in do , and the output will have a named column that contains all the graphs in a list.
library(dplyr) plots = mtcars %>% group_by(cyl) %>% do(plots = p %+% . + facet_wrap(~cyl)) Source: local data frame [3 x 2] Groups: <by row> cyl plots 1 4 <S3:gg, ggplot> 2 6 <S3:gg, ggplot> 3 8 <S3:gg, ggplot>
To see graphs in R, just ask about the column that contains the graphs.
plots$plots
And save as PDF
pdf() plots$plots dev.off()