英文:
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("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)
Thus far, I have been saving the temporary directory (But this is a lot of manual labour)
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="......")
答案1
得分: 3
你可以在你的函数中包含ggsave
,然后我们可以循环遍历split
的names()
:
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::map
和purrr::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("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)
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 <- function(df, nm) {
ggplot(df, aes(x=mpg, y=cyl)) +
geom_point() +
labs (title= paste(unique(df[["gear"]]), paste("- ", unique(df[["am"]]))))
}
# save plots to list
plots <- map(split, my_plot)
# loop over plots and save
iwalk(plots, ~ ggsave(paste0("my_path/mtcars_", .y, ".png"),
plot = .x))
<sup>Created on 2023-02-16 by the reprex package (v2.0.1)</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论