title: R: Plot multiple time series in a loop.Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ
body: <p>I'm trying to plot many time series from a database with 134 columns, one ts per colum and after that, save it in a .jpg file. I'm using a loop. I notice that the ploblem could be the <code>plot()</code> function.</p>
  
  <p>Some data with <code>dput()</code></p>
  
  <pre><code> structure(list(X9003 = c(3L, 8L, 6L, 6L, 2L, 3L, 6L, 2L, 6L, 
   3L), X9004 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 0L), X9007 = c(5L, 
   8L, 6L, 11L, 4L, 6L, 7L, 9L, 4L, 5L), X9009 = c(1L, 2L, 3L, 0L, 
   0L, 1L, 0L, 1L, 0L, 4L), X9010 = c(NA, NA, NA, NA, NA, NA, NA, 
   NA, 3L, 15L), X9012 = c(0L, 0L, 0L, 2L, 3L, 0L, 1L, 1L, 0L, 0L
   ), X9014 = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
   NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
   NA_integer_), X9016 = c(NA, NA, NA, NA, NA, NA, 12L, 27L, 7L, 
   12L), X9019 = c(3L, 8L, 27L, 38L, 19L, 27L, 25L, 47L, 5L, 3L), 
   X9020 = c(0L, 1L, 0L, 0L, 4L, 6L, 2L, 2L, 5L, 3L)), row.names = c(NA, 
   10L), class = "data.frame")
  </code></pre>
  
  <p>And </p>
  
  <pre><code>dput(dat_nombres[1:10,1:2])
  structure(list(ESTACIONES = c(9003L, 9004L, 9007L, 9009L, 9010L, 
  9012L, 9014L, 9016L, 9019L, 9020L), CLAVE = c(9003L, 9004L, 9007L, 
  9009L, 9010L, 9012L, 9014L, 9016L, 9019L, 9020L)), row.names = c(NA, 
  10L), class = "data.frame")
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>  data&lt;- read.csv("GranizoAnualCompleto_1961-2019_columnas.csv", header = 
     TRUE)
      dat_nombres&lt;-read.csv("Nombres_Estaciones_CLICOM.csv", header=TRUE )
      Estaciones&lt;-dat_nombres$ESTACIONES  # List
      Estacionestmp&lt;-data[,-1]
  
      # Loop
  
      for (i in seq_along (Estacionestmp)) { 
        serie&lt;-ts(Estacionestmp[,i], start = 1961, frequency = 1)
        mypath&lt;-file.path("C:","00 FENIX","03 Data", "Data_Est_Select_1961- 
         2019", "SeriesTiempo_GranizoAnual",  
                  paste("Ts_Granizo4_", Estaciones[i],".jpg", sep = "")) 
        jpeg(filename = mypath)  
  
        plot(serie,
           col="darkblue",
           main="Días con granizo 1961-2019 \n Estación", Estaciones[i] ,
           cex.main=2,
           sub=paste("Estación",Estaciones[i]),
           cex.sub= 1.5,
           col.main="darkred",
           xlab= "Tiempo",
           ylab= "Días con granizo",
           cex.lab=0.8,
           col.axis= "gray34",
           col.lab= "gray25",
           type= "l"
           )
         dev.off() }
  </code></pre>
  
  <p>The error message are:</p>
  
  <pre><code>      Error in xy.coords(x, y, xlabel, ylabel, log) : 
        'x' and 'y' lengths differ
  </code></pre>
  
  <p>But when I just code <code>plot(serie)</code> (without any details in the plot function) in the loop, there are no ploblem with the result and the files are created in the specific path that I have defined. Recently, I used this code but on a dataframe with 20 columns and it worked.</p>
  
  <p>Why that details in the plot function argument are causing the error message? How can I fix the loop for create multiple time series plots? Is there any better solution to do it?</p>
  
  <p>Thank you so much!</p>
  
  <p>EDIT: I find that the problem are when I trying to change the main for name and subpaste every plot, the problem with the <code>Estaciones[i]</code>; I prove write a <code>#</code> before for comment it, and now it works! So, I converted it to a list</p>
  
  <pre><code>  Estaciones_list&lt;-as.list(Estaciones) 
  </code></pre>
  
  <p>and tried with this new list in the main for every plot but it doesn't work either.</p>
  
  <p>Any suggestions?
  Thanks!</p>