R saving multiple ggplot2 plots as R

I would like to save multiple plots (with ggplot2) to a list during a large for-loop. And then subsequently display the images in a grid (with grid.arrange)

I have tried two solutions to this: 1 storing it in a list, like so:

pltlist[["qplot"]] <- qplot 

however for some reason this does save the plot correctly. So I resorted to a second strategy which is recordPlot() This was able to save the plot correctly, but unable to use it in a grid.

Reproducable Example:

require(ggplot2);require(grid);require(gridExtra)
df  <- data.frame(x = rnorm(100),y = rnorm(100))
histoplot <- ggplot(df, aes(x=x)) + geom_histogram(aes(y=..density..),binwidth=.1,colour="black", fill="white") 
qplot <- qplot(sample = df$y, stat="qq") 



pltlist <- list()
pltlist[["qplot"]] <- qplot
pltlist[["histoplot"]] <- histoplot
grid.arrange(pltlist[["qplot"]],pltlist[["histoplot"]], ncol=2) 

above code works but produces the wrong graph in my actual code

Then I tried recordPlot()

print(histoplot)
c1 <- recordPlot()
print(qplot)
c2 <- recordPlot()

I am able to display all the plots individually but grid.arrange produces an error:

grid.arrange(replayPlot(c1),replayPlot(c2), ncol=2) # = Error

Error in gList(list(wrapvp = list(x = 0.5, y = 0.5, width = 1, height = 1, : only 'grobs' allowed in "gList"

In this thread Saving grid.arrange() plot to file They dicuss a solution which utilizes arrangeGrob() instead

arrangeGrob(c1, c1, ncol=2) # Error

Error in vapply(x$grobs, as.character, character(1)) : values must be length 1, but FUN(X[[1]]) result is length 3

I am forced to use the recordPlot() instead of saving to a list since this does not produce the same graph when saved as when it is plotted immediately, which I unfortunately cannot replicate, sorry.

In my actual code I am doing a large for-loop, looping through several variables, making a correlation with each and making scatterplots, where I name the scatterplots dependent on their significans level. I then want to re-display the plots that were significant in a grid, in a dynamic knitr report.

I am aware that I could just re-plot the plots that were significant after the for-loop instead of saving them, (I can't save as png while doing knitr either). However I would like to find a way to dynammically save the plots as R-objects and then replot them in a grid afterwards.

Thanks for Reading

"R version 3.2.1" Windows 7 64bit - RStudio - Version 0.99.652 attached base packages: [1] grid grDevices datasets utils graphics stats methods base

other attached packages: [1] gridExtra_2.0.0 ggplot2_1.0.1


I can think of two solutions.

1. If your goal is to just save the list of plots as R objects, I recommend:

    saveRDS(object = pltlist, file = "file_path")

This way when you wish to reload in these graphs, you can just use readRDS() . You can then put them in cowplot or gridarrange . This command works for all lists and R Objects.

One caveat to this approach is if settings/labeling for ggplot2 is dependent upon things in the environment (not the data, but stuff like settings for point size, shape, or coloring) instead of the ggplot2 function used to make the graph), your graphs won't work until you restore your dependencies. One reason to save some dependencies is to modularize your scripts to make the graphs.

Another caveat is performance: From my experience, I found it is actually faster to read in the data and remake individual graphs than load in an RDS file of all the graphs when you have a large number of graphs (100+ graphs).

2. If your goal is to save an 'image' or 'picture' of each graph (single and/or multiplot as .png, .jpeg, etc.) , and later adjust things in a grid manually outside of R such as powerpoint or photoshop, I recommend:

    filenames <- c("Filename_1", "Filename_2") #actual file names you want... 
    lapply(seq_along(pltlist), function(i) {
      ggsave(filename = filenames[i], plot = pltlist[[i]], ...) #use your settings here
    })

Settings I like for single plots:

    lapply(seq_along(pltlist), function(i) ggsave(
      plot = pltlist[[i]], 
      filename = paste0("plot_", i, "_", ".tiff"), #you can even paste in pltlist[[i]]$labels$title
      device = "tiff", width=180, height=180, units="mm", dpi=300, compression = "lzw", #compression for tiff
      path = paste0("../Blabla") #must be an existing directory.
    ))

You may want to do the manual approach if you're really OCD about the grid arrangement and you don't have too many of them to make for publications. Otherwise, when you do grid.arrange you'll want to do all the specifications there (adjusting font, increasing axis label size, custom colors, etc.), then adjust the width and height accordingly.

链接地址: http://www.djcxy.com/p/5220.html

上一篇: 在ggplot中绘制RDA(素食主义者)

下一篇: R将多个ggplot2图作为R保存