I have a data set that I am trying to make a time series out of using the function plot_time_series within the package "timetk". The time sequence is in POSIXct date time format. My data however has gaps in between when there was no sampling effort. With that, I am trying to add shade regions to the time series plot where there was no effort. My script so far is as follows:
#lol is my time series plot
#newdf is the data frame it is utilizing
#datetime is the time sequence of my plot, already in POSIXct format
#mean_NASC is the y variable
lol <- newdf %>%
plot_time_series(datetime, mean_NASC,
.color_var = day(datetime),
.facet_ncol = 2,
.facet_scales = "free",
.interactive = FALSE,
.smooth = FALSE,
.title = "Mean NASC for Site",
.x_lab = "Date (1 hour intervals)",
.y_lab = "NASC",
.color_lab = "Day")
#make dataframe to utilize in geom_rect for shaded regions
shade = data.frame(x1=c("2016-08-18 05:00:00","2016-08-18 15:00:00"), x2=c("2016-08-24 01:00:00","2016-08-24 05:00:00"), y1=c(10,10), y2=c(150,150)
#change x1 and x2 from chr vector to POSIXct vector that matches format of datetime in newdf
shade$x1<-as.POSIXct(shade$x1, format="%Y-%m-%d %H:%M:%S", tz = "UTC")
shade$x2<-as.POSIXct(shade$x2, format="%Y-%m-%d %H:%M:%S", tz = "UTC")
#Attempt to add geom_rect shading to lol plot
lol2 <- lol + geom_rect(data=shade, aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2, fill="green", alpha=0.5,)
Running "lol" generates the correct time series plot. However, when I try to visualize "lol2" after running it, I am hit with the error
Error in FUN(X[[i]], ...) : object 'datetime' not found
What is the reason for why datetime is not being found in "lol2" when it is clearly showing in "newdf" and "lol"? Is this the proper way to add shaded regions to my time series plot utilizing the function plot_time_series? I would prefer not to use ggplot2 because I like the output of this function, however if ggplot2 is the only way to do this properly, could someone show me how I would go about that?