同时保存多个 ggplots

huangapple go评论63阅读模式
英文:

Saving multiple ggplots at the same time

问题

我正在运行许多不同数据子集上的图形,使用split函数和lapply函数。

我的问题是:如何轻松地保存所有图形?理想情况下,我想要将每个文件命名为图形的标题(表示数据子集)。

data("mtcars")

split <- split(mtcars, list(mtcars$gear, mtcars$am))

my_plot <- function(mtcars) {
  ggplot(mtcars, aes(x=mpg, y=cyl)) + 
  geom_point() +
  labs (title= paste(unique(mtcars$gear), paste("- ", unique(mtcars$am)))) }

lapply(split, my_plot)

到目前为止,我一直在保存临时目录(但这需要很多手动操作)。

plots.dir.path <- list.files(tempdir(), pattern="rs-graphics", full.names = TRUE); 
plots.png.paths <- list.files(plots.dir.path, pattern=".png", full.names = TRUE)
file.copy(from=plots.png.paths, to="......")
英文:

I am running a lot of graphs across different subsets of data, using the split function with the lapply function.

My problem is: How do I easily save all the graphs? Ideally I want to name each fileby the title of the graph (Indicating the subset of data).

data(&quot;mtcars&quot;)

split &lt;- split(mtcars, list(mtcars$gear, mtcars$am))

my_plot &lt;- function(mtcars) {
  ggplot(mtcars, aes(x=mpg, y=cyl)) + 
  geom_point() +
  labs (title= paste(unique(mtcars$gear), paste(&quot;- &quot;, unique(mtcars$am)))) }

lapply(split, my_plot)

Thus far, I have been saving the temporary directory (But this is a lot of manual labour)

plots.dir.path &lt;- list.files(tempdir(), pattern=&quot;rs-graphics&quot;, full.names = TRUE); 
plots.png.paths &lt;- list.files(plots.dir.path, pattern=&quot;.png&quot;, full.names = TRUE)
file.copy(from=plots.png.paths, to=&quot;......&quot;)

答案1

得分: 3

你可以在你的函数中包含ggsave,然后我们可以循环遍历splitnames()

data("mtcars")
library(ggplot2)

split <- split(mtcars, list(mtcars$gear, mtcars$am))

my_plot <- function(x) {
  p <- ggplot(split[[x]], aes(x=mpg, y=cyl)) + 
    geom_point() +
    labs (title= paste(unique(split[[x]][["gear"]]), paste(" - ", unique(split[[x]][["am"]])))
  
  ggsave(paste0("my_path/mtcars_", x, ".png"))
  p  
}

lapply(names(split), my_plot)

或者,我们可以使用稍微不同的工作流程,使用purrr::mappurrr::iwalk()。首先,我们使用purrr::map创建一个绘图列表,然后使用purrr::iwalk()循环遍历命名的绘图列表以保存它们:

library(purrr)

my_plot <- function(df, nm) {
  ggplot(df, aes(x=mpg, y=cyl)) + 
    geom_point() +
    labs (title= paste(unique(df[["gear"]]), paste(" - ", unique(df[["am"]])))
}

# 保存绘图到列表
plots <- map(split, my_plot)

# 循环遍历绘图并保存
iwalk(plots, ~ ggsave(paste0("my_path/mtcars_", .y, ".png"),
                     plot = .x))

创建于2023-02-16,使用reprex包 (v2.0.1)

英文:

You could include ggsave in your function, then we would loop over the names() of split instead:

data(&quot;mtcars&quot;)
library(ggplot2)

split &lt;- split(mtcars, list(mtcars$gear, mtcars$am))

my_plot &lt;- function(x) {
  p &lt;- ggplot(split[[x]], aes(x=mpg, y=cyl)) + 
    geom_point() +
    labs (title= paste(unique(split[[x]][[&quot;gear&quot;]]), paste(&quot;- &quot;, unique(split[[x]][[&quot;am&quot;]]))))
  
  ggsave(paste0(&quot;my_path/mtcars_&quot;, x, &quot;.png&quot;))
  p  
}

lapply(names(split), my_plot)

Alternatively we could use a slightly different workflow with purrr::map and purrr::iwalk(). First we create a list of plots with purrr::map and than loop over the named lists of plots using purrr::iwalk() to save them:

library(purrr)

my_plot &lt;- function(df, nm) {
  ggplot(df, aes(x=mpg, y=cyl)) + 
    geom_point() +
    labs (title= paste(unique(df[[&quot;gear&quot;]]), paste(&quot;- &quot;, unique(df[[&quot;am&quot;]]))))
}

# save plots to list
plots &lt;- map(split, my_plot)

# loop over plots and save
iwalk(plots, ~ ggsave(paste0(&quot;my_path/mtcars_&quot;, .y, &quot;.png&quot;),
                     plot = .x))

<sup>Created on 2023-02-16 by the reprex package (v2.0.1)</sup>

huangapple
  • 本文由 发表于 2023年2月16日 14:57:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定