'Saving patchwork ggplots with furrr future map only saves one of the plots

I am trying to save a list of patchworked ggplots (ie ggplots that have been wrapped together into 1 plot using the package patchwork).

My real data is creating several hundred plots so I would like to use furrr to speed it up. Using furrr::future_map works totally fine to save a standard ggplot however when I run it on a patchwork plot it is only saving one of the plots. See below with some default ggplots in place of my actual long list of plots.

The plot that is save by the furrr::map always seems to be the last plot (bottom right) in the patchwork plot.

Does anyone know why future_imap and imap are behaving differently here and it there is anything that can be done?

library(ggplot2)
library(patchwork)
library(purrr)
library(furrr)
#> Loading required package: future
plan(multisession, workers =  availableCores() - 1)

#make some example plots
p <- ggplot(mtcars, aes(wt, mpg))
p1 <- p + geom_point()
p2 <- p + geom_point(aes(colour = factor(cyl)))
p3 <- p + geom_point(aes(shape = factor(cyl)))
p4 <- p + geom_point(aes(size = qsec))

#make some example patchwork plots
allplots <- patchwork::wrap_plots(p1,p2,p3,p4)
allplots2 <- patchwork::wrap_plots(p1,p3,p2,p4)
allplots3 <- patchwork::wrap_plots(p3,p1,p2,p4)
allplots4 <- patchwork::wrap_plots(p4,p1,p2,p1)

#save list of standard ggplots to confirm future can output normal plots as expected
# These all look basically the same except small differences in resolution (not setting so not worried about this)
list_of_plots <- list("one"=p1,"two"=p2,"three"=p3,"four"=p4)
plots_save_imap <- imap(list_of_plots, ~ggsave(file.path("C:/temp/test",paste0(.y, ".png")), plot = .x))
#> Saving 7 x 5 in image
#> Saving 7 x 5 in image
#> Saving 7 x 5 in image
#> Saving 7 x 5 in image
plots_save_futureimap <- future_imap(list_of_plots, ~ggsave(file.path("C:/temp/test",paste0(.y, "future.png")), plot = .x))
#> Saving 7 x 7 in image
#> Saving 7 x 7 in image
#> Saving 7 x 7 in image
#> Saving 7 x 7 in image
# all look fine

#save list of patchworks

list_of_allplots <- list("first"= allplots,"second"= allplots2,"third"= allplots3,"fourth"= allplots4)

plots_save_imapPW <- imap(list_of_allplots, ~ggsave(file.path("C:/temp/test",paste0(.y, ".png")), plot = .x))
#> Saving 7 x 5 in image
#> Saving 7 x 5 in image
#> Saving 7 x 5 in image
#> Saving 7 x 5 in image

Example: Plot looks as expected with all 4 subplots

Plot looks as expected with all 4 subplots

plots_save_futureimapPW <- future_imap(list_of_allplots, ~ggsave(file.path("C:/temp/test",paste0(.y, "future1.png")), plot = .x))
#> Saving 7 x 7 in image
#> Saving 7 x 7 in image
#> Saving 7 x 7 in image
#> Saving 7 x 7 in image

Example: only one of the plots is output to each file ( Only one of the plots saves

If I just run

plots_futureimapPW <- future_imap(list_of_allplots, ~print(.x))
plots_futureimapPW

then plots_futureimapPW does give me the full patchworks within RStudio

Created on 2022-05-13 by the reprex package (v2.0.1)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source