Plot multiple time-series in a single graph - Error: Invalid input: date_trans works with objects of class Date only

Plot multiple time-series in a single graph - Error: Invalid input: date_trans works with objects of class Date only

我正在尝试将多个时间序列绘制成一个图形,如所附图像,但我收到此错误 错误:无效输入:date_trans 与 [=24 的对象一起使用=] 仅限日期。我试图通过使用 lubricate 包或我在互联网上找到的其他方法将包含月份的列转换为 date 格式来解决这个问题。这是数据:

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))

这是代码,其中没有我所有的尝试和错误:

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()

使用您的示例数据,您可以执行以下操作:

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