0

I am trying to loop through multiple headers using a for loop, in order to create multiple plots. I'm just not sure what I'm doing wrong when passing my header variable into my for loop, as I keep getting warnings and errors from R.

Here is the for loop I am using:

dflist <- c('ABStemp', 'ABSelec', 'ABSheat')

for (i in dflist) {
  plot1<-contourplot(i ~ hour * weekday | month,
                data = HourlyTotal,
                       cuts = 200,
                labels=TRUE,
                contour=FALSE,
               drop.unused.levels = lattice.getOption("drop.unused.levels"),
                region = TRUE,
                pretty=FALSE,
                xlab = "",
                ylab = "Day of Week",
                col.regions=colorRampPalette(c("blue","yellow","red")),
                main = "Absolute Error (Temperature)",
                       layout=c(2,4),
                       as.table= TRUE)

    plot2<-contourplot(i ~ hour * weekday,
                aspect=0.3,
                data = HourlyTotal,
                cuts = 200,
                #labels=TRUE,
                contour=FALSE,
                region = TRUE,
                pretty=FALSE,
                xlab = "Hour of Day",
                ylab = "Day of Week",
                col.regions=colorRampPalette(c("blue","yellow","red"))
                       )

    plot3<-contourplot(i ~ hour * month, 
                aspect=0.3,
                data = HourlyTotal,
                cuts = 200,
                #labels=TRUE,
                contour=FALSE,
                region = TRUE,
                pretty=FALSE,
                xlab = "Hour of Day",
                ylab = "Month of Year",
                col.regions=colorRampPalette(c("blue","yellow","red"))
                       )

pdf(paste('Rplot',i,'.pdf'), width=8, height=12) 
print(plot1, more=TRUE) 
print(plot2, more=TRUE) 
print(plot3, more=FALSE) 
dev.off()    

}

I have tried numerous variations on the above, but each time, the i variable fails to pass into the loop correctly, and I get NA's in each trellis plot.

The dataset I am using can be found here: SummaryData. Also, my full code is available online here: http://danielcoakley.com/projects/energy-simulation/

Thank You!

4

3 回答 3

1

您可以构建代表您的公式的字符串,然后使用函数创建formula对象。as.formula

as.formula(paste(i, "~ hour * weekday | month"))
于 2013-10-26T01:46:36.143 回答
1

感谢@zero323 的上述解决方案,以下代码现在可以完美运行。

dflist <- c('ABStemp', 'ABSelec')

for (i in dflist) {
  plot1<-contourplot(as.formula(paste(i, "~ hour * weekday | month")),
                data = HourlyTotal,
                       cuts = 200,
                labels=TRUE,
                contour=FALSE,
               drop.unused.levels = lattice.getOption("drop.unused.levels"),
                region = TRUE,
                pretty=FALSE,
                xlab = "",
                ylab = "Day of Week",
                col.regions=colorRampPalette(c("blue","yellow","red")),
                main = "Absolute Error (Temperature)",
                       layout=c(2,4),
                       as.table= TRUE)

    plot2<-contourplot(as.formula(paste(i, "~ hour * weekday")),
                aspect=0.3,
                data = HourlyTotal,
                cuts = 200,
                #labels=TRUE,
                contour=FALSE,
                region = TRUE,
                pretty=FALSE,
                xlab = "Hour of Day",
                ylab = "Day of Week",
                col.regions=colorRampPalette(c("blue","yellow","red"))
                       )

    plot3<-contourplot(as.formula(paste(i, "~ hour * month")), 
                aspect=0.3,
                data = HourlyTotal,
                cuts = 200,
                #labels=TRUE,
                contour=FALSE,
                region = TRUE,
                pretty=FALSE,
                xlab = "Hour of Day",
                ylab = "Month of Year",
                col.regions=colorRampPalette(c("blue","yellow","red"))
                       )

    plot4<-contourplot(as.formula(paste(i, "~ Meas.DryBlb * hour")), 
                aspect=1,
                       contour=FALSE,
                       region = TRUE,
                data = HourlyTotal,
                       )

    png(paste('Model\\Current Model\\Results\\',i,'.png'), width = 3600, height = 5000, units = "px", res = 400) 
    print(plot1, position = c(0,.5,1,1), more=TRUE)
    print(plot2, position = c(0,.25,1,.5), more=TRUE)
    print(plot3, position = c(0,0,1,.25), more=FALSE)
    dev.off()

}
于 2013-10-26T02:41:10.167 回答
0

问题是字符串"ABStemp"没有被解释为数据框中的名称。除了as.formula()解决方案之外,您还可以尝试字符串引用正常的上下文。

i在您的通话中替换contourplotHourlyTotal[,i]。这适用于ggplot. 不确定格子。

于 2013-10-26T01:59:39.857 回答