Save multiple ggplot2 plots as R object in list and re-displaying in grid

六眼飞鱼酱① 提交于 2019-12-24 03:34:09

问题


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


回答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.



来源:https://stackoverflow.com/questions/31947727/save-multiple-ggplot2-plots-as-r-object-in-list-and-re-displaying-in-grid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!