英文:
Save a list of plots from environment in R
问题
我想使用```ggsave()```从我的R环境中保存所有的图。**如何在R中保存环境中的图列表,然后将该列表用作**```ggsave()```的输入?
我从[这里][1]获取了一些关于```cars```的图来说明:
```R
PlotA <- ggplot(mtcars, aes(x=hp, y=mpg, color=cyl, shape=cyl)) +
geom_point(size=3)
PlotB <- ggplot(mtcars, aes(x=hp, y=mpg, color=cyl, shape=cyl)) +
geom_point(size=3) +
geom_smooth(method="lm", aes(fill=cyl))
PlotC <- ggplot(mtcars, aes(x=hp, y=mpg)) +
geom_point(size=3, aes(color=cyl, shape=cyl)) +
geom_smooth(method="loess", color="black", se=FALSE) +
geom_smooth(method="lm", aes(color=cyl, fill=cyl))
- 我的尝试:
saveplots <- list()
saveplots <- ls(pattern = 'Plot')
### 保存png文件 ###
for(i in 1:length(saveplots)){
ggsave(saveplots[[i]],
file=paste0("Total", saveplots,".png"),
width = 22, height = 11.5, units = "cm",
path = "plots/")
}
<details>
<summary>英文:</summary>
I want to save all my plots from my R environment with ```ggsave()```. **How can I save list of plots from environment in R and then use the list as input to** ```ggsave()``` ?
I got some plots with ```cars``` from [here][1] to illustrate:
PlotA <- ggplot(mtcars, aes(x=hp, y=mpg, color=cyl, shape=cyl)) +
geom_point(size=3)
PlotB <- ggplot(mtcars, aes(x=hp, y=mpg, color=cyl, shape=cyl)) +
geom_point(size=3) +
geom_smooth(method="lm", aes(fill=cyl))
PlotC <- ggplot(mtcars, aes(x=hp, y=mpg)) +
geom_point(size=3, aes(color=cyl, shape=cyl)) +
geom_smooth(method="loess", color="black", se=FALSE) +
geom_smooth(method="lm", aes(color=cyl, fill=cyl))
* My attempt:
saveplots <- list()
saveplots <- ls(pattern = 'Plot')
Save pngs
for(i in 1:length(saveplots)){
ggsave(saveplots[[i]],
file=paste0("Total", saveplots,".png"),
width = 22, height = 11.5, units = "cm",
path = "plots/")
}
* Some posts helped a bit, but not quite yet ([ex1][2] [ex2][3]). Any ideas? Thanks in adv.
[1]: https://www.datavis.ca/courses/RGraphics/R/gg-cars.html
[2]: https://stackoverflow.com/questions/49167607/r-get-object-from-global-environment-from-function-if-object-exists-in-global-bu
[3]: https://stackoverflow.com/questions/50599366/r-get-objects-from-environment-and-feed-to-function
</details>
# 答案1
**得分**: 2
你可以使用函数 `get` 从环境中获取对象。
```R
for (i in 1:length(saveplots)) {
ggsave(plot = get(saveplots[[i]]),
filename = paste0("Total", saveplots[[i]], ".png"),
width = 22, height = 11.5, units = "cm",
path = "plots/")
}
英文:
You can use the function get
to get the object from the environment.
for(i in 1:length(saveplots)){
ggsave(plot = get(saveplots[[i]]),
filename=paste0("Total", saveplots[[i]],".png"),
width = 22, height = 11.5, units = "cm",
path = "plots/")
}
答案2
得分: 2
另一种类似的方法是使用lapply
与ls
和get
:
plots_list <- lapply(ls(pattern="Plot"), get)
lapply(seq_along(plots_list), function(i) {
ggsave(paste0("Total", i, ".png"), plots_list[[i]], width=22, height=11.5, units="cm", path = "plots/")
})
英文:
Another similar way using lapply
with ls
and get
:
plots_list <- lapply(ls(pattern="Plot"), get)
lapply(seq_along(plots_list), function(i) {
ggsave(paste0("Total", i, ".png"), plots_list[[i]], width=22, height=11.5, units="cm", path = "plots/")
})
答案3
得分: 2
使用 mget
library(purrr)
library(dplyr)
library(stringr)
mget(savePlots) %>%
iwalk(~ ggsave(str_c("Total", .y, ".png"),
.x, width = 22, height = 11.5, units = "cm", path = "plots/"))
英文:
Using mget
library(purrr)
library(dplyr)
library(stringr)
mget(savePlots) %>%
iwalk(~ ggsave(str_c("Total", .y, ".png"),
.x, width = 22, height = 11.5, units = "cm", path = "plots/"))
答案4
得分: 1
"Alternative if you don't know/remember/care what the names of all your ggplot
s are, try saveplots <- lsclass('ggplot')
. It's available in my package (at CRAN) 'cgwtools'; source provided here:
lsclass <- function (type = "numeric")
{
inlist <- ls(.GlobalEnv)
classlist <- sapply(1:length(inlist), function(j) class(get(inlist[j])))
tnams <- sapply(1:length(inlist), function(j) type %in% classlist[[j]])
return(inlist[tnams])
}
"
英文:
Alternative if you don't know/remember/care what the names of all your ggplot
s are, try saveplots <- lsclass('ggplot')
. It's available in my package (at CRAN) "cgwtools" ; source provided here:
lsclass <- function (type = "numeric")
{
inlist <- ls(.GlobalEnv)
classlist <- sapply(1:length(inlist), function(j) class(get(inlist[j])))
tnams <- sapply(1:length(inlist), function(j) type %in% classlist[[j]])
return(inlist[tnams])
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论