I am trying to get geom_area to have similar colors depending on the value of the y axis.
So, for example, if we have geom_area with a maximum value of X 50, I would like the fill color to be blue when below 10 and red when above 10.
Data examples;
df <- data.frame(
y = sample(1:50),
x = sample(1:50)
)
The closest I've reached so far is to use the following code:
ggplot(data = df, aes(x = x)) +
geom_area(aes(y = y),fill = "red") +
geom_ribbon(aes(ymin = 0, ymax = ifelse(y >= 10,10,y)),fill = "blue")
This almost gives me what I demand, but the problem is that the horizontal split does not completely cross the geometry, because when the next value falls below the maximum value, the edge of the tape goes straight to the next point, which breaks the crack.

, , , , .