I have some time series data, hourly, that runs across 5 years. I would like to plot my time series data with year as a grouping variable, so there are 5 lines across all dates and times of the year. Simply plotting dateTime on the x-axis and using lubridate::year(dateTime) as the grouping/colour in aes makes one long line with 5 different colours on it.
rough example data;
require(data.table)
require(lubridate)
require(ggplot2)
# this is just 7 days of hourly data, over 3 separate years
dt <- data.table(date = c(seq(as.Date("2018-03-01"), as.Date("2018-03-07"),by="day"), seq(as.Date("2019-03-01"), as.Date("2019-03-07"),by="day"), seq(as.Date("2020-03-01"), as.Date("2020-03-07"),by="day")), hr = rep(1:24, 21))
dt[, value := sin(hr)*sample(1:3,1)]
dt[, dateTime := as.POSIXct(paste0(date," ",hr,":00:00"), format="%Y-%m-%d %H:%M")]
# the result should be an x-axis of 7 days/hours, with three lines for years.
# the below is obviously not that
ggplot(dt, aes(x=dateTime,y=value,group=year(dateTime), colour=year(dateTime)))+
geom_line()
I thought there would be a way of formatting posix times as just month/day/time without the year component, but it appears to just return NA.
(p.s. not really interested in grouping by yday, for example, as i want the intricacies of the hourly cycles to be plotted)
