(Very much a novice, so please excuse any confusion/obvious mistakes)
Goal: A loop that allows me to plot multiple maps, displaying density data (D
) for grid cells, across multiple months and seasons. The data for each month, season, etc., are in 8 separate columns; the loop would run through the columns of the data frame (DF)
Tried: Adding the plot from each iteration of the loop to a list so all plots can be called up to be displayed in a multipanel figure.
out <- NULL
for(i in 1:8){
D <- DF[,i]
x <- names(DF)[i]
p <-ggplot() + geom_polygon(data=DF, aes(x=long, y=lat, group=Name, fill=D), colour = "lightgrey") +labs(title=x)
out[[i]]<- p
print(p)
}
Problem: Even though the print(p)
yields the correct plot for each iteration, the plots in the list out
display the data from the final loop only.
So, when I try to use grid.arrange
with plots in "out", all plots show the same data (from the 8th column); however, the plots do retain the correct title. When I try to call up each plot - e.g., print(out[[1]])
, shows the same plot - except for the title label - as print(out[[8]])
.
It seems that the previous elements in the list are being overwritten with each loop? However, the title of the plots seem to display correctly.
Is there something obviously wrong with how I'm constructing the out
list? How can I avoid having each previous plot overwritten?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…