I want to use ggplot2 to produce a graph like the one below, where the area between the line and a reference value is shaded, and the color of the shading depends on whether y is greater or less than the reference value.
Here's a reprex using the LakeHuron dataset, setting 579 as the reference value, and using geom_ribbon()
to do the shading. This gets close, but I only want to fill the area between the line and the reference line.
huron <- data.frame(year = 1875:1972, level = as.vector(LakeHuron))
ggplot(data = huron, aes(x = year)) +
geom_ribbon(aes(ymin = 579, ymax = level, fill = level > 579)) +
geom_line(aes(y = level)) +
geom_hline(yintercept = 579)
Does anybody have any ideas? Thanks in advance.