title: shading month intervals when plotting time series data with different start and end points using ggplot
body: <p>I am trying to shade winter months using <code>geom_rect()</code> in <code>ggplot2</code> and have a number of different time series data with varying start and end points.  For each time series I want the period from November - March of every year to be shaded (the remaining months should receive no coloring).</p>
  
  <p>For example, with the following time series </p>
  
  <pre><code>library(ggplot2)
  library(lubridate)
  
  now &lt;- Sys.time()
  set.seed(123)
  datOne &lt;- data.frame(IndID = "One",
                        DateTime = seq(from = now - dyears(0.2), length.out = 50, by = "months"),
                        Value = rnorm(50))
  </code></pre>
  
  <p>I can define a new data frame object with the starting and ending point of each shading section (i.e. Nov - Mar for every year)</p>
  
  <pre><code>temp &lt;- data.frame(
    start = as.Date(c('2016-11-1', '2017-11-01', '2018-11-01', '2019-11-01')), 
    end   = as.Date(c('2017-03-01', '2018-03-01', '2019-03-01', '2020-03-01')))
  
  dateRanges &lt;- data.frame(
    start = as.POSIXct(temp [,1], "%Y-%m-%d")+ hours(6),
    end   = as.POSIXct(temp [,2], "%Y-%m-%d")+ hours(6))
  </code></pre>
  
  <p>And then make a plot.</p>
  
  <pre><code>ggplot(datOne) + 
    geom_rect(data = dateRanges, aes(xmin = start , xmax = end, ymin = -Inf, ymax = Inf),
              inherit.aes=FALSE, alpha = 0.4, fill = c("lightblue"))+
    geom_line(aes(x=  DateTime, y = Value), size = 1.5)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/EG5LI.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/EG5LI.jpg" alt="enter image description here"></a></p>
  
  <p>While this works fine for a single time series, I am making a separate figure for each of many individual time series, each of which have different start and end points and require a unique data frame from which to create shading regions.
  For example, as pictured below the same <code>dateRanges</code> data frame applied to a different time series obviously does not work.  </p>
  
  <pre><code>datTwo &lt;- data.frame(IndID = "Two",
                        DateTime = seq(from = now , length.out = 100, by = "months"),
                        Value = rnorm(100)) 
  
  ggplot(datTwo) + 
    geom_rect(data = dateRanges, aes(xmin = start , xmax = end, ymin = -Inf, ymax = Inf),
              inherit.aes=FALSE, alpha = 0.4, fill = c("lightblue"))+
    geom_line(aes(x=  DateTime, y = Value), size = 1.5)
  </code></pre>
  
  <p>Is there a different way to shade the two time series so that for each I can either automate the creation of <code>dateRanges</code> or use a different method all together (i.e. annotate...).  Said differently, given the need to make roughly 100 different figures with 100 different time series over different periods, what is the best way to shade the period from Nov - Mar for any given year?</p>
  
  <p>Thanks in advance.</p>
  
  <p><a href="https://i.stack.imgur.com/rFXqC.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/rFXqC.jpg" alt="enter image description here"></a></p>