R在循环中将多个图形打印到多个文件

时间:2016-01-20 16:35:09

标签: r loops plot

我现在已经解决了这个问题2天了。

我试图将6个绘图添加到页面并在循环中打印到文件中...

我很想弄清楚如何循环......我已经尝试过多次安排,但每次我都这么近但不完全相同。我通过参数(DO,PH,NH4等......)和6个时间段(6月到7月)生成绘图。所以6个图表到每个参数的页面(总共26个)。我可以成功地将6个图表添加到页面并手动保存到文件中......但是这需要大量手动保存,我即将解决类似但更大的项目。

到目前为止,我已经获得了将每个单独的绘图成功打印到文件的代码,但是没有将6个绘图打印到要提交的页面。我得到的最好的是它创建一个文件然后循环并覆盖每个页面,这样我就有一个标记为Chla的页面,其中6个时间段的水温度被绘制出来。

这是我现在的代码....我觉得我非常接近解决方案我能尝到它。

void myClass::myFunction(double& a,  double& b, double *c[24], double *d[30], int& e)

这是所有人的头和尾。

alls<-subset( all,all$Layer=="S ")

x <- levels( alls$Parameter)
a <- levels( alls$period)

for( h in 1:length(x)){
   png( paste( x[h], "_plot", ".jpeg", sep = ""), width = 1000, height = 800)

   par( mfrow=c( 2,3),mar=c( 1.5,.5,.5,1),oma=c( 1,1,1,1),mgp=c( 1.5, .5, 0),cex = 1.3)

for( i in 1:length( x ) ){
         y <- alls[ alls$Parameter == x[ i ],]

for( j in 1:length( a ) ){
         b <- y[ y$period == a[ j ],]

plot( b$mean~b$YEAR,main = paste( x[ i ], "_RET_",a[ j ],"_Surface" ),cex.main=.6,subset=b$Station=="RET1.1",pch='.',  xlab="",ylab="",xlim=c( 1985,2015),ylim=c( min( b$mean ),max( b$mean ) ),cex.axis=.6)
 lines( b$mean~b$YEAR,subset= b$Station=="RET1.1",col="black")
 lines( b$mean~b$YEAR,subset= b$Station=="RET2.1",col="blue")
 lines( b$mean~b$YEAR,subset= b$Station=="RET1.1",col="red")
 lines( b$mean~b$YEAR,subset= b$Station=="RET2.1",col="red")
 lines( b$mean~b$YEAR,subset= b$Station=="RET2.2",col="green")
 lines( b$mean~b$YEAR,subset= b$Station=="RET2.4",col="orange")
 lines( b$mean~b$YEAR,subset= b$Station=="RET3.1",col="purple")
 lines( b$mean~b$YEAR,subset= b$Station=="RET3.2",col="pink")
 lines( b$mean~b$YEAR,subset= b$Station=="RET4.1",col="dark green")
 lines( b$mean~b$YEAR,subset= b$Station=="RET4.2",col="light blue")
 lines( b$mean~b$YEAR,subset= b$Station=="RET4.3",col="dark blue")
 lines( b$mean~b$YEAR,subset= b$Station=="RET5.1A",col="violet")
 lines( b$mean~b$YEAR,subset= b$Station=="RET5.2",col="gray")

    }
         }
   dev.off()
             } 

我抬头看了......我做对了吗?

     YEAR Layer Station     mean  period Parameter
121 1986    S   RET1.1 7.700000 Apr_May        DO
122 1987    S   RET1.1 7.700000 Apr_May        DO
123 1988    S   RET1.1 8.375000 Apr_May        DO
124 1989    S   RET1.1 8.275000 Apr_May        DO
125 1990    S   RET1.1 8.425000 Apr_May        DO
126 1991    S   RET1.1 7.125000 Apr_May        DO
127 1992    S   RET1.1 9.350000 Apr_May        DO
128 1993    S   RET1.1 8.225000 Apr_May        DO
129 1994    S   RET1.1 8.150000 Apr_May        DO
130 1995    S   RET1.1 8.233333 Apr_May        DO
131 1996    S   RET1.1 9.500000 Apr_May        DO
132 1997    S   RET1.1 9.050000 Apr_May        DO
133 1998    S   RET1.1 7.575000 Apr_May        DO
134 1999    S   RET1.1 8.675000 Apr_May        DO
135 2000    S   RET1.1 8.150000 Apr_May        DO
136 2001    S   RET1.1 7.625000 Apr_May        DO
137 2002    S   RET1.1 7.725000 Apr_May        DO
138 2003    S   RET1.1 7.600000 Apr_May        DO
139 2004    S   RET1.1 8.500000 Apr_May        DO
140 2005    S   RET1.1 8.425000 Apr_May        DO
114235 2006    S  RET5.1A 26.48333    all  TURB_NTU
114236 2007    S  RET5.1A 16.00833    all  TURB_NTU
114237 2008    S  RET5.1A 20.16923    all  TURB_NTU
114238 2009    S  RET5.1A 21.37250    all  TURB_NTU
114239 2015    S  RET5.1A 16.23333    all  TURB_NTU
114255 1993    S   RET5.2 53.81875    all  TURB_NTU
114256 1994    S   RET5.2 27.14000    all  TURB_NTU
114257 1995    S   RET5.2 40.60333    all  TURB_NTU
114258 1996    S   RET5.2 47.48333    all  TURB_NTU
114259 1999    S   RET5.2 23.46250    all  TURB_NTU
114260 2000    S   RET5.2 24.94545    all  TURB_NTU
114261 2001    S   RET5.2 27.58333    all  TURB_NTU
114262 2002    S   RET5.2 23.07500    all  TURB_NTU
114263 2003    S   RET5.2 41.76667    all  TURB_NTU
114264 2004    S   RET5.2 25.63636    all  TURB_NTU
114265 2005    S   RET5.2 35.23846    all  TURB_NTU
114266 2006    S   RET5.2 36.36364    all  TURB_NTU
114267 2007    S   RET5.2 25.62500    all  TURB_NTU
114268 2008    S   RET5.2 25.12500    all  TURB_NTU
114269 2009    S   RET5.2 27.37000    all  TURB_NTU

1 个答案:

答案 0 :(得分:0)

我认为我发现了您的问题,它是par的位置。没有足够的数据来执行多个循环但是,这应该按照您的预期方式运行。

alls<-subset( all,all$Layer=="S ")

x <- levels( alls$Parameter)
a <- levels( alls$period)

# NOTE: You had an unnecessary loop here. The h loop.

 for( i in 1:length( x ) ){

png( paste( x[i], "_plot", ".jpeg", sep = ""), width = 1000, height = 800)

y <- alls[ alls$Parameter == x[ i ],]

# Par occurs right before plotting begins

par( mfrow=c( 2,3),mar=c( 1.5,.5,.5,1),oma=c( 1,1,1,1),mgp=c( 1.5, .5, 0),cex = 1.3)

 for( j in 1:length( a ) ){

  b <- y[ y$period == a[ j ],]

  plot( b$mean~b$YEAR,main = paste( x[ i ], "_RET_",a[ j ],"_Surface" ),cex.main=.6,subset=b$Station=="RET1.1",pch='.',  xlab="",ylab="",xlim=c( 1985,2015),ylim=c( min( b$mean ),max( b$mean ) ),cex.axis=.6)
  lines( b$mean~b$YEAR,subset= b$Station=="RET1.1",col="black")
  lines( b$mean~b$YEAR,subset= b$Station=="RET2.1",col="blue")
  lines( b$mean~b$YEAR,subset= b$Station=="RET1.1",col="red")
  lines( b$mean~b$YEAR,subset= b$Station=="RET2.1",col="red")
  lines( b$mean~b$YEAR,subset= b$Station=="RET2.2",col="green")
  lines( b$mean~b$YEAR,subset= b$Station=="RET2.4",col="orange")
  lines( b$mean~b$YEAR,subset= b$Station=="RET3.1",col="purple")
  lines( b$mean~b$YEAR,subset= b$Station=="RET3.2",col="pink")
  lines( b$mean~b$YEAR,subset= b$Station=="RET4.1",col="dark green")
  lines( b$mean~b$YEAR,subset= b$Station=="RET4.2",col="light blue")
  lines( b$mean~b$YEAR,subset= b$Station=="RET4.3",col="dark blue")
  lines( b$mean~b$YEAR,subset= b$Station=="RET5.1A",col="violet")
  lines( b$mean~b$YEAR,subset= b$Station=="RET5.2",col="gray")

}
  dev.off()
}