保存R环境中的绘图列表

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

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&#39)

### 保存png文件 ###

for(i in 1:length(saveplots)){
  ggsave(saveplots[[i]],
         file=paste0("Total", saveplots,".png"),
         width = 22, height = 11.5, units = "cm",
         path = "plots/")
}
  • 一些帖子有所帮助,但还不够 (ex1 ex2). 有什么建议吗?先谢谢。

<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(&quot;Total&quot;, saveplots[[i]],&quot;.png&quot;),
         width = 22, height = 11.5, units = &quot;cm&quot;,
         path = &quot;plots/&quot;)
}

答案2

得分: 2

另一种类似的方法是使用lapplylsget

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 &lt;- lapply(ls(pattern=&quot;Plot&quot;), get)

lapply(seq_along(plots_list), function(i) {
  ggsave(paste0(&quot;Total&quot;, i, &quot;.png&quot;), plots_list[[i]], width=22, height=11.5, units=&quot;cm&quot;, path = &quot;plots/&quot;)
})

答案3

得分: 2

使用 mget

library(purrr)
library(dplyr)
library(stringr)
mget(savePlots) %&gt;%
    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) %&gt;%
    iwalk(~ ggsave(str_c(&quot;Total&quot;, .y, &quot;.png&quot;), 
               .x, width = 22, height = 11.5, units = &quot;cm&quot;, path = &quot;plots/&quot;))

答案4

得分: 1

"Alternative if you don't know/remember/care what the names of all your ggplots 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 ggplots are, try saveplots &lt;- lsclass(&#39;ggplot&#39;) . It's available in my package (at CRAN) "cgwtools" ; source provided here:

lsclass &lt;- function (type = &quot;numeric&quot;) 
{
    inlist &lt;- ls(.GlobalEnv)
    classlist &lt;- sapply(1:length(inlist), function(j) class(get(inlist[j])))
    tnams &lt;- sapply(1:length(inlist), function(j) type %in% classlist[[j]])
    return(inlist[tnams])
}

huangapple
  • 本文由 发表于 2023年2月19日 21:13:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500380.html
匿名

发表评论

匿名网友

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

确定