保存几个ggplots到文件 [英] Save several ggplots to files

查看:93
本文介绍了保存几个ggplots到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要保存一组不断变化的ggplot文件。为此,我使用一个如下所示的for循环:

I want to save a changing set of ggplot is different files. To do this I use a for-loop looking something like this:

save = c("plot1","plot2")
for (i in 1:length(save)){
  ggsave(cat(save[i],"\n"), file="i.pdf")
}

plot1和 plot2是工作的ggplots(=绘图对象的名称)。因为出现以下错误:

"plot1" and "plot2" are working ggplots (=names of the plot objects). Because I got the following error:

Error in ggsave(cat(save[i], "\n"), file = "i.pdf") : 
  plot should be a ggplot2 plot

猫功能。有或没有函数,它都会返回相同的错误。如果我直接输入地块,它就可以工作...

I tried the cat-function. It returns the same error with or without the function. If I enter the "plot" directly it works...

我在做什么错了?

(编辑了示例,所以有多个图)

(Edited the example so there is more than one plot)

推荐答案

您需要在ggsave中指定参数图:

You need to specify the argument plot in ggsave :

ggsave(plot = plot, file = "save.pdf")

如果您有多个ggplot,则需要先将它们保存在列表中。

If you have several ggplot you need to save them in a list first.

plotlist = list()
plotlist[[1]] = plot1
plotlist[[2]] = plot2

等。或任何其他方式。一旦找到列表,您就可以在其上循环:

etc. Or any other way. Once you end up with the list you can loop on it :

for(i in 1:2){
  ggsave(plot = plot[[i]], file = paste("file",i,".pdf",sep=""))
}

这样可以将绘图保存在file1 file2等中。

That will save you the plots in file1 file2 etc.

这篇关于保存几个ggplots到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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