Automatic way to highlight parts of a time series plot that have values higher than a certain threshold?

前端 未结 2 516

I\'m looking for an automatic way of highlighting some portions of the plot that have Station2 values greater than a pre-defined threshold which is 0 in this case.

2条回答
  •  执念已碎
    2021-01-24 08:47

    Something like:

    library(dplyr)
    dateRanges <- df %>% 
      mutate(Date2 = lead(Date)) %>% 
      filter(key == 'Station2', value > 0 | lead(value) > 0, Date2 - Date == 1)
    
    gg1 +
      geom_rect(data = dateRanges, 
                aes(xmin = Date, xmax = Date2, ymin = -Inf, ymax = Inf), 
                inherit.aes = FALSE,
                color = NA,
                fill = 'grey20',
                alpha = 0.2)
    

    It's easiest to just draw one rect for per day.

提交回复
热议问题