使用Furrr未来地图保存拼布GGPLOTS仅保存其中一个图
我正在尝试保存拼打ggplots的列表(即使用软件包patchwork
将已包装到1个图中的ggplots)。
我的真实数据是创建数百个图,因此我想使用furrr
将其加快。使用furrr :: future_map
可以很好地保存标准的GGPLOT,但是当我在patchwork
绘制绘图上时,它只是保存其中一个图。请参阅下面的一些默认GGPLOT,以代替我实际的长图。
由furrr :: Map
保存的图总是是拼布图中的最后一个图(右下)。
有谁知道为什么future_imap
和imap
在这里表现不同,并且可以做任何事情?
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
示例:所有4个子图
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
如果我刚刚运行
plots_futureimapPW <- future_imap(list_of_allplots, ~print(.x))
plots_futureimapPW
,则plots_futureimappw
确实为我提供了在2022-05-13创建的rstudio 中的完整patchworks
,由 reprex软件包(v2.0.1)
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
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 (
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论