将ls中的多个ggplots保存为一个,并将单独的文件保存在R中

编程入门 行业动态 更新时间:2024-10-27 17:20:56
本文介绍了将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)) 等。

(未经测试)

/ 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中

本文发布于:2023-08-05 11:23:54,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   并将   保存为   文件   ls

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!