Basically geom_rect
is drawing rectangles, one for each row, on top of each other, thus rendering the object opaque. Check this answer and this link.
Alternative 1
Using annotate
instead of geom_rect
ggplot(df1) +
annotate("rect", xmin = min_date, xmax = max_date, ymin = -Inf, ymax = 3, fill = "palegreen", alpha = 0.2) +
geom_line(aes(x = date, y = pb, colour = "P/B")) +
geom_line(aes(x = date, y = return_index, colour = "return"))
Alternative 2
Removing the argument data = df1
from ggplot
and add it to the required layers.
ggplot() +
geom_rect(aes(xmin = min_date, xmax = max_date, ymin = -Inf, ymax = 3), fill = "palegreen", alpha = 0.2) +
geom_line(data= df1, aes(x = date, y = pb, colour = "P/B")) +
geom_line(data= df1, aes(x = date, y = return_index, colour = "return"))
Output: