R:使用for循环保存多个ggplots [英] R: saving multiple ggplots using a for loop

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

问题描述

我想自动生成许多ggplots:
$ b

通用数据集:

  mydata <-data.frame(matrix(rnorm(100),ncol = 5))
名称(mydata)<-c(Tijd,X1,X2 ,X3,X4)

指定要包含的变量:

 开始= 2 
停止= 5


$
$ b

  gvec< -vector(list,length = length(b $ b 

开始:停止))

创建图表:

< pre $ lt; -ggplot(mydata,aes_string(x =Tijd,y = names(mydata)[i]))pre> + geom_point()+ mytheme
gvec [[i-Start + 1]]< -graphy
}

保存图表:

pre $ for(i in Start:Stop){
tiff paste0(Test / Residu / Plots / Prei / mydata。,names(mydata)[i],09.14.tiff),width = 720,height = 720)
gvec [[i-Start + 1 ]]
graphics.off()
}

生成的地块;我也可以手动保存地块。但是,使用最后一个循环生成的文件都是空白的。我无法弄清楚这个原因。

根据Roland的建议,我尝试了
print(gvec [[i-Start + 1]])但我仍然得到空白文件作为输出。

解决方案

这是一个在循环中创建ggplots的完全可重复的例子。

 #在循环中分隔ggplot图形。 
library(ggplot2)

#使变量名列表循环。
var_list = combn(names(iris)[1:3],2,simplify = FALSE)


plot_list = list()
(i in 1:3){
p = ggplot(iris,aes_string(x = var_list [[i]] [1],y = var_list [ (大小= 3,aes(color =物种))
plot_list [[i]] = p
}

#把地块保存到tiff。为每个绘图创建一个单独的文件。
for(i in 1:3){
file_name = paste(iris_plot_,i,.tiff,sep =)
tiff(file_name)
print (plot_list [[i]])
dev.off()
}

#另一个选项:创建PDF,其中每个页面是一个单独的图。
pdf(plots.pdf)
(i in 1:3){
print(plot_list [[i]])
}
dev.off ()


I want to automate the generation of a number of ggplots:

Generic dataset:

mydata<-data.frame(matrix(rnorm(100),ncol=5))
names(mydata)<-c("Tijd","X1","X2","X3","X4") 

Specify variables to include:

Start=2
Stop=5

List to save the plots in:

gvec<-vector("list",length=length(Start:Stop))

Create plots:

for(i in Start:Stop){
  graphy<-ggplot(mydata,aes_string(x="Tijd",y=names(mydata)[i]))+geom_point()+mytheme
  gvec[[i-Start+1]]<-graphy
}

Save plots:

for(i in Start:Stop){
tiff(paste0("Test/Residu/Plots/Prei/mydata.",names(mydata)[i],"09.14.tiff"),width=720,height=720)
gvec[[i-Start+1]]
graphics.off()
}

The list of plots is generated; I can save the plots manually as well. However, using the last loop the files generated are all blank. I can't figure out the reason for this.

As per Roland's suggestion I tried print(gvec[[i-Start+1]]) but I still get blank files as output.

解决方案

Here is a fully reproducible example of creating ggplots in a loop.

# Plot separate ggplot figures in a loop.
library(ggplot2)

# Make list of variable names to loop over.
var_list = combn(names(iris)[1:3], 2, simplify=FALSE)

# Make plots.
plot_list = list()
for (i in 1:3) {
    p = ggplot(iris, aes_string(x=var_list[[i]][1], y=var_list[[i]][2])) +
        geom_point(size=3, aes(colour=Species))
    plot_list[[i]] = p
}

# Save plots to tiff. Makes a separate file for each plot.
for (i in 1:3) {
    file_name = paste("iris_plot_", i, ".tiff", sep="")
    tiff(file_name)
    print(plot_list[[i]])
    dev.off()
}

# Another option: create pdf where each page is a separate plot.
pdf("plots.pdf")
for (i in 1:3) {
    print(plot_list[[i]])
}
dev.off()

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

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