使用Furrr未来地图保存拼布GGPLOTS仅保存其中一个图

发布于 2025-01-28 12:11:34 字数 3100 浏览 3 评论 0原文

我正在尝试保存拼打ggplots的列表(即使用软件包patchwork 将已包装到1个图中的ggplots)。

我的真实数据是创建数百个图,因此我想使用furrr将其加快。使用furrr :: future_map可以很好地保存标​​准的GGPLOT,但是当我在patchwork绘制绘图上时,它只是保存其中一个图。请参阅下面的一些默认GGPLOT,以代替我实际的长图。

furrr :: Map保存的图总是是拼布图中的最后一个图(右下)。

有谁知道为什么future_imapimap在这里表现不同,并且可以做任何事情?

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文