df <- data.frame(
Date = c(1,2,3,4,5,6,7,8,9),
Wholesale = c(100, 105, 110, 105, 115, 107, 120, 115, 110))
I'm trying to colour the areas above a geom_line but below a geom_hline in one colour and the area below a geom_line but above the geom_hline in a different colour
I thought I could achieve this using different geom_ribbons but the data doesn't match the geom_line.
ggplot(df, aes(x = Date)) +
geom_line(aes(y = Wholesale), group = 1)+
geom_ribbon(aes(ymin = min(Wholesale), ymax =Wholesale), fill = 'grey', position = 'identity') +
geom_ribbon(aes(ymin=pmin(Wholesale,110), ymax=110), fill="black", col="black", alpha=0.5) +
geom_ribbon(aes(ymin=110, ymax=pmax(Wholesale,110)), fill="red", col="red", alpha=0.5) +
geom_hline(yintercept = 110, linetype = "dashed", size = 1)
I think the problem might be that the pmin() and pmax() functions only consider the values in the Wholesale column but not the values in the geom_line() layer. As a result, the black and red ribbons are drawn between the minimum and maximum values of the Wholesale column, regardless of the values in the geom_line() layer.
Is there any other way to colour certain areas without having to define each area manually?