首页 > 解决方案 > Error when using for loop in R: 'plot.new has not been called yet'

问题描述

I have made a graph that works fine when I run the code without a for loop. When the same graph is a part of a for loop, I get the error message "plot.new has not been called yet" and no graph. How can I run this graph in a for loop?

There are very many questions about this error message. I have read more than 15 of them carefully and tried several things, but with no success. For example, I looked at the problem described in plot.new has not been called yet. However, I cannot see how my code could close the device when I use a for loop, but not when it is run without the for loop.

The plot consists of one plot.xts() and then several lines().

GRA_events <- c("20080118/20080201", "20080123/20080206", "20080205/20080219", "20080210/20080224", "20080229/20080314", "20080912/20080926", "20081002/20081016", "20081206/20081220", "20081218/20090101", "20090209/20090223")

for (i in 1:length(GRA_events)) {
  #i=1
  period <- GRA_events[i]

  png(paste("GRA_",i,".png", sep=""), width = 1200, height = 600)
  windows(width = 16, height = 8)
  # plot(0, 0, xlab="", ylab="")
  par(mfrow=c(1,1), oma=c(4.5, 1.5, 4, 2.5), mar=c(0.1,4,0.1,0.1), cex=1, las=1)
  plot.xts(GRA_mod$Qprecip, on=1,
           main =period, ylim=c(0,10),
           col = "blue", type="l", lwd=2,
           ylab="Precip. & flow [m3/10min]",
           yaxis.right = FALSE)
  lines(GRA_mod$Qin, on=1, col = "black", type="l", lwd = 2)
  addLegend("topright", on=1,
            legend.names = c("Flow (obs)", "Precipitation", "Flow (model)"),
            lty=c(1, 1, 1), lwd=c(2, 2, 1),
            col=c("black", "blue", "red"))
  lines(GRA_all_part$Air.temp, on=NA,  main =period, ylim=c(-15,25), 
        col = "red", type="l", lwd=2, ylab="Air temp. [degree C]")
  lines(GRA_all_part$zero_line, on=2, col = "grey", lwd=2, lty=2)
  lines(GRA_all_part$SnowDepth, on=2,  main =period, col = "blue", lwd=2,
        ylab="Snow depth")
  addLegend("topright", on=2,
            legend.names = c("Snow depth", "Air temperature"),
            lty=c(1,1), lwd=c(2,2),
            col=c("blue","red"))
  lines(GRA_all_part$LW_ground, on=NA,  main =period, col = "green", 
        lwd=2, ylim=c(165,400))
  lines(GRA_all_part$LW_in, on=3,  main =period, col = "black", lwd=2)
  addLegend("topright", on=3,
            legend.names = c("Longwave in", "Longwave ground"),
            lty=c(1,1), lwd=c(1,1),
            col=c("black","green"))
  lines(GRA_all_part$SW_ground, on=NA,  main =period, col = "red", lwd=2,
        ylim=c(-2,425))
  lines(GRA_all_part$Tot_rad, on=4,  main =period, col = "black", lwd=3)
  addLegend("topright", on=4,
            legend.names = c("Total radiation", "Shortwave ground"),
            lty=c(1,1), lwd=c(3,1),
            col=c("black","red"))
  title(main = "Available data", outer=TRUE)
  dev.copy(which = 2)
  dev.list()
  dev.off()
}

When I skip the first (for (i in 1:length(GRA_events)) {) and the last row (}) in the code and instead start at i=1, the graph is nicely plotted both on screen and on a .png-file, see figure below. When I use the for loop, I get the error message.

successful graph, when the for loop is not used

Finally, some data to reproduce the error (much shorter, but it gives the same problem):

GRA_mod <- structure(c(0.05856, -0.07752, 0.099, -0.16374, 0.26532, -0.22224, 
-0.05352, -0.06132, 0.18216, 0.01536, -0.00222, 0.07116, -0.00996, 
-0.04068, 0.14706, 0.00318, -0.04626, -0.0105, -0.09654, 0.00762, 
0.1026, 0.01314, -0.08766, 0.03522, -0.03792, -0.00972, -0.00174, 
-0.0354, 0.07428, -0.1035, -0.00468, 0.05442, 0.01062, 0.06954, 
-0.04374, -0.02568, -0.0435, -0.02772, 0.05766, 0.03222, -0.02136, 
-0.04164, 0.10182, -0.01296, 0.0675, 0.021, 0.08514, 0.02994, 
-0.02862, 0.03162, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), class = c("xts", 
"zoo"), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "GMT", tclass = c("POSIXct", 
"POSIXt"), tzone = "GMT", index = structure(c(1202169600, 1202170200, 
1202170800, 1202171400, 1202172000, 1202172600, 1202173200, 1202173800, 
1202174400, 1202175000, 1202175600, 1202176200, 1202176800, 1202177400, 
1202178000, 1202178600, 1202179200, 1202179800, 1202180400, 1202181000, 
1202181600, 1202182200, 1202182800, 1202183400, 1202184000, 1202184600, 
1202185200, 1202185800, 1202186400, 1202187000, 1202187600, 1202188200, 
1202188800, 1202189400, 1202190000, 1202190600, 1202191200, 1202191800, 
1202192400, 1202193000, 1202193600, 1202194200, 1202194800, 1202195400, 
1202196000, 1202196600, 1202197200, 1202197800, 1202198400, 1202199000
), tzone = "GMT", tclass = c("POSIXct", "POSIXt")), .Dim = c(50L, 
2L), .Dimnames = list(NULL, c("Qin", "Qprecip")))
GRA_all_part <- structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, -0.12, 
-0.12, -0.12, -0.12, -0.12, -0.12, -0.07, -0.07, -0.07, -0.07, 
-0.07, -0.07, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, -0.08, -0.08, 
-0.08, -0.08, -0.08, -0.08, -0.22, -0.22, -0.22, -0.22, -0.22, 
-0.22, -0.18, -0.18, -0.18, -0.18, -0.18, -0.18, -0.2, -0.2, 
-0.2, -0.2, -0.2, -0.2, -0.25, -0.25, 298.07, 298.07, 298.07, 
298.07, 298.07, 298.07, 299.35, 299.35, 299.35, 299.35, 299.35, 
299.35, 299.98, 299.98, 299.98, 299.98, 299.98, 299.98, 300.38, 
300.38, 300.38, 300.38, 300.38, 300.38, 300.98, 300.98, 300.98, 
300.98, 300.98, 300.98, 302.35, 302.35, 302.35, 302.35, 302.35, 
302.35, 302.07, 302.07, 302.07, 302.07, 302.07, 302.07, 297.35, 
297.35, 297.35, 297.35, 297.35, 297.35, 297.93, 297.93, 278.25, 
278.25, 278.25, 278.25, 278.25, 278.25, 283.78, 283.78, 283.78, 
283.78, 283.78, 283.78, 286.83, 286.83, 286.83, 286.83, 286.83, 
286.83, 286.68, 286.68, 286.68, 286.68, 286.68, 286.68, 287.82, 
287.82, 287.82, 287.82, 287.82, 287.82, 289.92, 289.92, 289.92, 
289.92, 289.92, 289.92, 286.13, 286.13, 286.13, 286.13, 286.13, 
286.13, 259.12, 259.12, 259.12, 259.12, 259.12, 259.12, 271.43, 
271.43, -0.98, -0.98, -0.98, -0.98, -0.98, -0.98, -0.83, -0.83, 
-0.83, -0.83, -0.83, -0.83, -0.67, -0.67, -0.67, -0.67, -0.67, 
-0.67, -0.97, -0.97, -0.97, -0.97, -0.97, -0.97, -0.77, -0.77, 
-0.77, -0.77, -0.77, -0.77, -0.85, -0.85, -0.85, -0.85, -0.85, 
-0.85, -0.97, -0.97, -0.97, -0.97, -0.97, -0.97, -1, -1, -1, 
-1, -1, -1, -0.97, -0.97, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2.865, -2.73, 
-2.704, -2.703, -2.67, -2.487, -2.447, -2.443, -2.303, -2.406, 
-2.507, -2.506, -2.499, -2.475, -2.459, -2.383, -2.372, -2.425, 
-2.512, -2.566, -2.457, -2.41, -2.413, -2.393, -2.393, -2.377, 
-2.383, -2.366, -2.316, -2.3, -2.175, -2.022, -1.99, -1.914, 
-1.898, -1.87, -1.845, -1.812, -1.841, -1.93, -1.998, -1.995, 
-2.083, -2.253, -2.463, -2.633, -2.647, -2.446, -2.376, -2.596, 
0.874, 0.874, 0.874, 0.873, 0.873, 0.873, 0.872, 0.873, 0.873, 
0.872, 0.871, 0.871, 0.871, 0.871, 0.87, 0.87, 0.87, 0.87, 0.871, 
0.87, 0.869, 0.87, 0.869, 0.869, 0.869, 0.868, 0.868, 0.868, 
0.869, 0.868, 0.868, 0.868, 0.868, 0.868, 0.869, 0.869, 0.868, 
0.868, 0.867, 0.869, 0.869, 0.868, 0.867, 0.866, 0.865, 0.864, 
0.864, 0.865, 0.865, 0.866, -2.567, -2.48, -2.47, -2.474, -2.373, 
-2.207, -2.245, -2.133, -2.059, -2.3, -2.35, -2.335, -2.313, 
-2.308, -2.245, -2.165, -2.16, -2.255, -2.336, -2.334, -2.183, 
-2.197, -2.149, -2.171, -2.135, -2.143, -2.114, -2.064, -1.999, 
-1.955, -1.768, -1.659, -1.657, -1.625, -1.633, -1.597, -1.543, 
-1.508, -1.531, -1.595, -1.532, -1.524, -1.67, -1.799, -1.9, 
-2.025, -2.051, -2.01, -1.998, -2.136, 0.976, -1.292, 1.65, -2.729, 
4.422, -3.704, -0.892, -1.022, 3.036, 0.256, -0.037, 1.186, -0.166, 
-0.678, 2.451, 0.053, -0.771, -0.175, -1.609, 0.127, 1.71, 0.219, 
-1.461, 0.587, -0.632, -0.162, -0.029, -0.59, 1.238, -1.725, 
-0.078, 0.907, 0.177, 1.159, -0.729, -0.428, -0.725, -0.462, 
0.961, 0.537, -0.356, -0.694, 1.697, -0.216, 1.125, 0.35, 1.419, 
0.499, -0.477, 0.527, 0.195, 0.27, 0.199, 0.247, 0.237, 0.203, 
0.224, 0.187, 0.191, 0.256, 0.242, 0.256, 0.242, 0.271, 0.22, 
0.276, 0.215, 0.233, 0.251, 0.22, 0.26, 0.237, 0.232, 0.233, 
0.224, 0.228, 0.233, 0.228, 0.215, 0.228, 0.237, 0.238, 0.216, 
0.191, 0.22, 0.187, 0.203, 0.207, 0.199, 0.203, 0.22, 0.216, 
0.229, 0.211, 0.216, 0.22, 0.229, 0.22, 0.211, 0.211, 1873, 1872, 
1873, 1871, 1874, 1871, 1871, 1870, 1872, 1872, 1872, 1872, 1872, 
1871, 1873, 1873, 1872, 1872, 1871, 1871, 1871, 1871, 1870, 1871, 
1870, 1870, 1870, 1869, 1870, 1869, 1868, 1869, 1869, 1869, 1869, 
1868, 1868, 1867, 1868, 1868, 1868, 1867, 1868, 1868, 1868, 1869, 
1869, 1869, 1869, 1869, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), class = c("xts", 
"zoo"), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "GMT", tclass = c("POSIXct", 
"POSIXt"), tzone = "GMT", index = structure(c(1202169600, 1202170200, 
1202170800, 1202171400, 1202172000, 1202172600, 1202173200, 1202173800, 
1202174400, 1202175000, 1202175600, 1202176200, 1202176800, 1202177400, 
1202178000, 1202178600, 1202179200, 1202179800, 1202180400, 1202181000, 
1202181600, 1202182200, 1202182800, 1202183400, 1202184000, 1202184600, 
1202185200, 1202185800, 1202186400, 1202187000, 1202187600, 1202188200, 
1202188800, 1202189400, 1202190000, 1202190600, 1202191200, 1202191800, 
1202192400, 1202193000, 1202193600, 1202194200, 1202194800, 1202195400, 
1202196000, 1202196600, 1202197200, 1202197800, 1202198400, 1202199000
), tzone = "GMT", tclass = c("POSIXct", "POSIXt")), .Dim = c(50L, 
15L), .Dimnames = list(NULL, c("SnowDepth", "SnowCover", "SnowCoverMountains", 
"SW_ground", "LW_ground", "LW_in", "Tot_rad", "Precip", "Sun.temp", 
"Soil.temp", "Air.temp", "Qin", "Qout", "V_med", "zero_line")))

标签: r

解决方案


推荐阅读