plotting multiple deseasonalised time series in R

Plotting a whole dataframe of time series is easy in R. However, what about plotting series that you treated?

For example, if you have N series (N > 1) that you have treated for example by deseasonalising it. The concatenation of strings to make name variables is not as easy in python. Therefore, it is harder if you have several time series to put on the same plot. The solution here is to loop through the dataframe to append the plot with the lines for the time series.

#Your Time series are in dataframe. We will call it here "datadf"
#loop for all the series
# njgc.01 à  8 et ngjc.60 à  67
s = 1 #the first column you want to use.
t = 0 #number of useless columns
e = ncol(datadf)
seriesname = paste("name", s - t, e - t, sep = "_") #concatenation of the name and the number of the columns
png(filename=paste0(seriesname, ".png"), width = 1800, height = 830, units = "px")
for (i in s:e)
TS = ts(datadf[i], frequency=365)
dec = decompose(TS, type="multiplicative") #seasonal component
series = TS / dec$seasonal #deseasonalisation
j = i - (s - 1)
if (j < 2){
plot.ts(series, col = j) #there is a color number corresponding to each time series.
lines(series, col = j)
#assign(paste("series", j, sep = "_"), TS / dec$seasonal)
legend("bottomleft", legend=colnames(datadf[s:e]), col=1:(e-(s-1)), lty=1, cex=.65)


Add Comment

Required fields are marked *. Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.