保存R环境中的绘图列表

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

Save a list of plots from environment in R

问题

  1. 我想使用```ggsave()```从我的R环境中保存所有的图。**如何在R中保存环境中的图列表,然后将该列表用作**```ggsave()```的输入?
  2. 我从[这里][1]获取了一些关于```cars```的图来说明:
  3. ```R
  4. PlotA <- ggplot(mtcars, aes(x=hp, y=mpg, color=cyl, shape=cyl)) +
  5. geom_point(size=3)
  6. PlotB <- ggplot(mtcars, aes(x=hp, y=mpg, color=cyl, shape=cyl)) +
  7. geom_point(size=3) +
  8. geom_smooth(method="lm", aes(fill=cyl))
  9. PlotC <- ggplot(mtcars, aes(x=hp, y=mpg)) +
  10. geom_point(size=3, aes(color=cyl, shape=cyl)) +
  11. geom_smooth(method="loess", color="black", se=FALSE) +
  12. geom_smooth(method="lm", aes(color=cyl, fill=cyl))
  • 我的尝试:
  1. saveplots <- list()
  2. saveplots <- ls(pattern = 'Plot&#39)
  3. ### 保存png文件 ###
  4. for(i in 1:length(saveplots)){
  5. ggsave(saveplots[[i]],
  6. file=paste0("Total", saveplots,".png"),
  7. width = 22, height = 11.5, units = "cm",
  8. path = "plots/")
  9. }
  • 一些帖子有所帮助,但还不够 (ex1 ex2). 有什么建议吗?先谢谢。
  1. <details>
  2. <summary>英文:</summary>
  3. 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()``` ?
  4. 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))

  1. * 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/")
}

  1. * Some posts helped a bit, but not quite yet ([ex1][2] [ex2][3]). Any ideas? Thanks in adv.
  2. [1]: https://www.datavis.ca/courses/RGraphics/R/gg-cars.html
  3. [2]: https://stackoverflow.com/questions/49167607/r-get-object-from-global-environment-from-function-if-object-exists-in-global-bu
  4. [3]: https://stackoverflow.com/questions/50599366/r-get-objects-from-environment-and-feed-to-function
  5. </details>
  6. # 答案1
  7. **得分**: 2
  8. 你可以使用函数 `get` 从环境中获取对象。
  9. ```R
  10. for (i in 1:length(saveplots)) {
  11. ggsave(plot = get(saveplots[[i]]),
  12. filename = paste0("Total", saveplots[[i]], ".png"),
  13. width = 22, height = 11.5, units = "cm",
  14. path = "plots/")
  15. }
英文:

You can use the function get to get the object from the environment.

  1. for(i in 1:length(saveplots)){
  2. ggsave(plot = get(saveplots[[i]]),
  3. filename=paste0(&quot;Total&quot;, saveplots[[i]],&quot;.png&quot;),
  4. width = 22, height = 11.5, units = &quot;cm&quot;,
  5. path = &quot;plots/&quot;)
  6. }

答案2

得分: 2

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

  1. plots_list <- lapply(ls(pattern="Plot"), get)
  2. lapply(seq_along(plots_list), function(i) {
  3. ggsave(paste0("Total", i, ".png"), plots_list[[i]], width=22, height=11.5, units="cm", path = "plots/")
  4. })
英文:

Another similar way using lapply with ls and get:

  1. plots_list &lt;- lapply(ls(pattern=&quot;Plot&quot;), get)
  2. lapply(seq_along(plots_list), function(i) {
  3. 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;)
  4. })

答案3

得分: 2

使用 mget

  1. library(purrr)
  2. library(dplyr)
  3. library(stringr)
  4. mget(savePlots) %&gt;%
  5. iwalk(~ ggsave(str_c("Total", .y, ".png"),
  6. .x, width = 22, height = 11.5, units = "cm", path = "plots/"))
英文:

Using mget

  1. library(purrr)
  2. library(dplyr)
  3. library(stringr)
  4. mget(savePlots) %&gt;%
  5. iwalk(~ ggsave(str_c(&quot;Total&quot;, .y, &quot;.png&quot;),
  6. .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:

  1. lsclass <- function (type = "numeric")
  2. {
  3. inlist <- ls(.GlobalEnv)
  4. classlist <- sapply(1:length(inlist), function(j) class(get(inlist[j])))
  5. tnams <- sapply(1:length(inlist), function(j) type %in% classlist[[j]])
  6. return(inlist[tnams])
  7. }

"

英文:

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:

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

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:

确定