0

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?

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 04 '21 at 00:55
  • Welcome to SO! To help us to help would you mind making your issue reproducible by sharing a sample of your data or provide some fake data? See [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). To share your data, type `dput(NAME_OF_DATASET)` into the console and copy & paste the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 20))` for the first twenty rows of data. – stefan Sep 04 '21 at 07:43
  • ... but I would try with adding argument `inherit.aes=FALSE` to `geom_rect` to prevent that global aes set via `plot_time_series` are passed to `geom_rect` which I guess is the reason for your error. – stefan Sep 04 '21 at 07:45

0 Answers0