So I've looked at a few other questions related to this: Storing Plot Objects in a List; Store Plots in List in Loop to produce a list of ggplot2 objects.
I was trying a for loop to create and assign ggplot2 parameters to a list that I could call later to create a grid of graphs.
Time <- as.POSIXct(origin="1970-01-01",seq(1522461060,1522467000,by=60),tzone="UTC")
P <- abs(rnorm(100,0.0028,sd=0.038))
Qmin <- abs(rnorm(100,0.007,0.0021))
RE.24hr <- sort(rep(1:20,5))
dt1 <- data.table(Time,P,Qmin,RE.24hr)
require(ggplot2)
require(data.table)
REL <- max(dt1$RE.24hr)
hydl <- list()
maxp <- max(dt1$P); maxq <- max(dt1$Qmin,na.rm=T)
i <- 1
My for loop ends up with NULL being assigned to all but the last entry of the list:
for(i in REL){
mydata <- subset(dt1,RE.24hr==i)
hydl[[i]] <- ggplot(mydata,aes(x=Time)) + geom_line(aes(y=Qmin),colour='blue') +
geom_line(aes(y=P*10)) + scale_y_continuous(limits=c(0,maxp*10),sec.axis = sec_axis(~./10,name="Precip [m]")) +
theme_bw() + theme(axis.title=element_blank())
plot(hydl[[i]])
}
do.call(grid.arrange, c(hydl, ncol = 5))
However, when I switched to a while loop logic the code executed as expected:
while(i <= REL){
mydata <- subset(dt1,RE.24hr==i)
hydl[[i]] <- ggplot(mydata,aes(x=Time)) + geom_line(aes(y=Qmin),colour='blue') +
geom_line(aes(y=P*10)) + scale_y_continuous(limits=c(0,maxp*10),sec.axis = sec_axis(~./10,name="Precip [m]")) +
theme_bw() + theme(axis.title=element_blank())
plot(hydl[[i]])
i <- i + 1
}
do.call(grid.arrange, c(hydl, ncol = 5))
I am confused as to why switching the logical structure from a for loop to a while loop would change how assignment to a list is achieved. My best guess is that I'm overlooking a basic, fundamental coding principle.
R version 3.4.2 (2017-09-28) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 17134)