What am I doing wrong here please? I'm trying to shade alternate 24-hr daily rectangles with transparent gray. But only the last rectangle from the for-loop gets drawn(?!?) If I do things manually instead of by for-loop it works fine.
Is there a way to vectorize this to avoid the for-loop? (And can it be done with qplot?) I'm new to ggplot2 and yes I read through Hadley's site, book and examples.
Second issue: the alpha setting on the aesthetic doesn't prevent the rectangles occluding the background. How to get transparency?
dat <- data.frame(my_x_series=1:192, my_y_series=5.0*runif(192))
# (ymin, ymax are computed for this series using min/max(na.rm==TRUE))
ymax <- 5.0
ymin <- 0.0
p <- ggplot(dat, aes(x=my_x_series,alpha=0.9))
alternate_daily_bars_xmin <- c(4,52,100,148)
for (shade_xmin in alternate_daily_bars_xmin) {
shade_xmax <- min(shade_xmin+24, 192) # clamp at end of x-range
p <- p + geom_rect(aes(alpha=0.5,xmin=shade_xmin,xmax=shade_xmax,ymin=ymin,ymax=ymax), fill='gray80')
}
p <- p + geom_point(aes(y=my_y_series))
p