Home > Mobile >  Plot multiple time-series in a single graph - Error: Invalid input: date_trans works with objects of
Plot multiple time-series in a single graph - Error: Invalid input: date_trans works with objects of

Time:05-06

I am trying to plot multiple time series into a single graph like the attached image, but I am getting this error Error: Invalid input: date_trans works with objects of class Date only. I tried to solve the issue by converting the column containing the Months into a date format either using the lubricate package, or other methods I found on internet. Here is the data:

structure(list(Month = c("2016-01-01", "2016-02-01", "2016-03-01", 
"2016-04-01", "2016-05-01", "2016-10-01", "2016-11-01", "2016-12-01"
), Residential = c(36.22021, 40.31832, 37.83721, 39.75821, 34.7028, 
42.97021, 37.61029, 41.58934), Residential.Informal = c(33.3279, 
35.2452, 34.67857, 30.44121, 30.65073, 35.55425, 34.04282, 35.22392
), Non.Residential = c(79.75459, 80.79518, 96.62088, 95.32751, 
94.49208, 91.24384, 115.131, 119.9929)), class = "data.frame", row.names = c(NA, 
-8L))

here is the code, without all my try-and-error in it:

df <- read.csv("wd/ts_all_month.csv")
df_melt = melt(df, id.vars = 'Month')

#multiple ts plots
tiff("ts_lu.tiff", units="in", width=14, height=8, res=300)
ggplot(df_melt, aes(x = Month, y = value))   
    geom_line()   
    facet_wrap(~ variable, scales = 'free_y', ncol = 1)
dev.off()

time-series plots

CodePudding user response:

With your sample data you can do:

ggplot(df_melt, aes(x = Month, y = value))   
    geom_line()   
    facet_wrap(~ variable, scales = 'free_y', ncol = 1)

enter image description here

  • Related