Provide shades between dates on the x axis

I have a graph in R with date / time (POSIXct) on the X axis and some data on the Y axis.
I want to provide a hue on the x axis between about 3pm and 6PM of each date on the x axis

+5
source share
2 answers

More or less what Brian Diggs offers above ,

#sample data
set.seed(666)
dat <- data.frame(x = seq(as.POSIXct('2011-03-27 00:00:00'), 
len= (n=24), by="1 hour"), y = cumsum(rnorm(n)))
#Breaks for background rectangles
rects <- data.frame(xstart = as.POSIXct('2011-03-27 15:00:00'), 
xend = as.POSIXct('2011-03-27 18:00:00'))

library(ggplot2)
ggplot() + 
  geom_rect(data = rects, aes(xmin = xstart, xmax = xend, 
            ymin = -Inf, ymax = Inf), alpha = 0.4) + 
  geom_line(data = dat, aes(x,y))

Would give you that m

+15
source

Create data.frame with columns that are 3 and 6 pm for each day, covering the data. Use this for the layer geom_rect(using Infand -Inffor y). Place this layer in front of the data layers (so that it is below them) and let it fill in the alpha so that you can see the grid through it.

, .

+7

All Articles