R在循环中覆盖多个图

时间:2015-10-05 22:09:06

标签: r plot overlay

所以我创建了一个循环,可以生成10个单独的图:

for (k in 1:nrow(sites)) {
  temp_title <- paste("site",k, "county", sites[k,2],"site",sites[k,3])
  l <- which(hourly_nj_table$County.Code==sites[k,2]&hourly_nj_table$Site.Num==sites[k,3])#grab data for each site individually
  temp_filename <- paste("/Users/bob111higgins/Documents/School/College/Rutgers/Atmospheric Research",temp_title,".pdf")
  PM_site <- hourly_nj_table[l,]
  PM_site$realTime <- as.numeric(PM_site$Time.Local)
  PM_mean_site <- aggregate(PM_site, by=list(PM_site$Time.Local),FUN="mean",na.rm=TRUE)  #Make it average by time of day so can make time series plots. 
  plot(PM_mean_site$realTime,PM_mean_site$Sample.Measurement, type="l",lwd=10,main=paste(temp_title),xlab="LocalTime",ylab="Ozone (ppm)")#,ylim=c(0,0.05))
}

但我想看看他们如何在同一轴上进行比较。通常(如果我只是硬编码)我会添加一个新参数,然后创建下一个图,但我不确定如何将其合并到一个循环中。

如果有帮助,数据全部来自一个csv文件。

谢谢!

1 个答案:

答案 0 :(得分:0)

你真的很亲密。 Plot()让球滚动,lines()将允许你在情节内画出:

for (k in 1:nrow(sites)) {
  temp_title <- paste("site",k, "county", sites[k,2],"site",sites[k,3])
  l <- which(hourly_nj_table$County.Code==sites[k,2]&hourly_nj_table$Site.Num==sites[k,3])#grab data for each site individually
  temp_filename <- paste("/Users/bob111higgins/Documents/School/College/Rutgers/Atmospheric Research",temp_title,".pdf")
  PM_site <- hourly_nj_table[l,]
  PM_site$realTime <- as.numeric(PM_site$Time.Local)
  PM_mean_site <- aggregate(PM_site, by=list(PM_site$Time.Local),FUN="mean",na.rm=TRUE)  #Make it average by time of day so can make time series plots. 
  ifesle(k ==1 , 
  plot(PM_mean_site$realTime,PM_mean_site$Sample.Measurement, type="l",lwd=10,main=paste(temp_title),xlab="LocalTime",ylab="Ozone (ppm)")#,ylim=c(0,0.05)),
  lines(PM_mean_site$realTime,PM_mean_site$Sample.Measurement, lwd=10))
}

我确信有更好的方法可以解决这个问题,但这就是我过去做过的事情。