To plot your rectangles, create a data frame where each row contains the coordinates for a single rectangle. This construct works for all polygons, not just rectangles. Once you know this, it's easy to avoid the loop.
Then, just be careful whether you map a variable to an aesthetic or not. In your case, you need to set alpha
to whatever value you wish, so it does not form part of the aes()
settings.
library(ggplot2)
dat <- data.frame(my_x_series=1:192, my_y_series=5.0*runif(192))
rect_left <- c(4,52,100,148)
rectangles <- data.frame(
xmin = rect_left,
xmax = rect_left + 24,
ymin = 0,
ymax = 5
)
ggplot() +
geom_rect(data=rectangles, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
fill='gray80', alpha=0.8) +
geom_point(data=dat, aes(x=my_x_series, y=my_y_series))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…