将ls中的多个ggplots保存为一个,并将单独的文件保存在R中 [英] Saving multiple ggplots from ls into one and separate files in R

查看:265
本文介绍了将ls中的多个ggplots保存为一个,并将单独的文件保存在R中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的ls上有几个ggplots作为对象。我想将它们保存为单独的文件(尽管我也有兴趣知道如何将它们全部保存在1个大文件中)。我已阅读此:问题,但我似乎无法调整代码。我也尝试将它们全部绘制在一个大文件中,如 do.call错误(grid.arrange,plots2 [[i]]):第二个参数必须是一个列表。



这是我到目前为止所尝试的:

 > ls()#我的ls上的对象列表。所有的p *都是我想要保存的ggplots。 
[1]alldatdat2dat3datadlookdlook2dlook3ilook2mdfx
[12] orderpp1p10p11p12p13p14p15p16
[23]p17p18p19p2 p3p4p5p6p7p8p9

>对象< -ls()
> plot_objects [14:30]
> pre pre pre pre pre pre pre pre pre pre pre pre pre $ p $% p5p6p7p8p9

> class(plot)
[1]character

plots2< -as.list(plots)#转换为列表。

library(gridExtra)#Code建议创建一个pdf文件。
pdf(test.pdf,onefile = TRUE)
for(i seq(length(plots2))){
do.call(grid.arrange,plots2 [ i]])
}
dev.off()


解决方案

然后您可以简单地逐页打印它们,

  pdf(all.pdf)
不可见(lapply(l,print))
dev.off()

或者为每个文件保存一个图,

 无形(mapply(ggsave,file = paste0(plot-,names(l),.pdf),plot = l))

或将它们全部放在一个页面中,

  ggsave(arrange.pdf ,arrangeGrob(grobs = l))

或者将它们排列在多个页面中2x2,

  ggsave(arrange2x2.pdf,marrangeGrob(grobs = l,nrow = 2,ncol = 2))


$ b

等。

(未经测试)

/ p>

I have several ggplots as objects on my ls. I want to save them as separate files (although I would also be interested to know how to save them all under 1 big file). I have read this: question and question but I can't seem to adapt the code. I also tried to plot them all in one big file as suggested here but do get this error: Error in do.call("grid.arrange", plots2[[i]]) : second argument must be a list. There's something that I am missing in getting all the ggplots in one list.

This is what I've tried so far:

> ls() #List of objects on my ls. All the p* are my ggplots that I want to save.
[1] "all"     "dat"     "dat2"    "dat3"    "data"    "dlook"   "dlook2"  "dlook3"  "i"       "look2"   "mdfx"   
[12] "objects" "order"   "p"       "p1"      "p10"     "p11"     "p12"     "p13"     "p14"     "p15"     "p16"    
[23] "p17"     "p18"     "p19"     "p2"      "p3"      "p4"      "p5"      "p6"      "p7"      "p8"      "p9"    

> objects<-ls()
> plot<-objects[14:30]
> plots
 [1] "p1"  "p10" "p11" "p12" "p13" "p14" "p15" "p16" "p17" "p18" "p19" "p2"  "p3"  "p4"  "p5"  "p6"  "p7"  "p8"  "p9" 

> class(plots)
[1] "character"

plots2<-as.list(plots)#Transform into a list. 

library(gridExtra) #Code suggested to create one pdf file.
pdf("test.pdf", onefile = TRUE)
for (i in seq(length(plots2))) {
  do.call("grid.arrange", plots2[[i]])  
}
dev.off()

解决方案

it's best to have your plots in a list

l = mget(plots)

Then you can simply print them page-by-page,

pdf("all.pdf")
invisible(lapply(l, print))
dev.off()

or save one plot per file,

invisible(mapply(ggsave, file=paste0("plot-", names(l), ".pdf"), plot=l))

or arrange them all in one page,

ggsave("arrange.pdf", arrangeGrob(grobs = l))

or arrange them 2x2 in multiple pages,

ggsave("arrange2x2.pdf", marrangeGrob(grobs = l, nrow=2, ncol=2))

etc.

(untested)

这篇关于将ls中的多个ggplots保存为一个,并将单独的文件保存在R中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆