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 plot()
function.
Some data with dput()
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")
And
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")
My code:
data<- read.csv("GranizoAnualCompleto_1961-2019_columnas.csv", header =
TRUE)
dat_nombres<-read.csv("Nombres_Estaciones_CLICOM.csv", header=TRUE )
Estaciones<-dat_nombres$ESTACIONES # List
Estacionestmp<-data[,-1]
# Loop
for (i in seq_along (Estacionestmp)) {
serie<-ts(Estacionestmp[,i], start = 1961, frequency = 1)
mypath<-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() }
The error message are:
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ
But when I just code plot(serie)
(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.
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?
Thank you so much!
EDIT: I find that the problem are when I trying to change the main for name and subpaste every plot, the problem with the Estaciones[i]
; I prove write a #
before for comment it, and now it works! So, I converted it to a list
Estaciones_list<-as.list(Estaciones)
and tried with this new list in the main for every plot but it doesn't work either.
Any suggestions? Thanks!