I am trying to colour ribbons in ggplot2. When using geom_ribbon, I am able to specify ymin and ymax and a fill color. What it now does is coloring everything that is between ymin and ymax with no regard to upper Limit or lower Limit.
Example (modified from Internet):
library("ggplot2")
# Generate data (level2 == level1)
huron <- data.frame(year = 1875:1972, level = as.vector(LakeHuron), level2 = as.vector(LakeHuron))
# Change Level2
huron[1:50,2] <- huron[1:50,2]+100
huron[50:90,2] <- huron[50:90,2]-100
h <- ggplot(huron, aes(year))
h +
geom_ribbon(aes(ymin = level, ymax = level2), fill = "grey80") +
geom_line(aes(y = level)) + geom_line(aes(y=level2))
I'd like to fill the area, where (ymin > ymax), with a different colour than where (ymin < ymax). In my real data I have export and import values. There, I'd like to color the area where export is higher than import green, where import is bigger than export I want the ribbon to be red.
Alternative: I'd like geom_ribbon to only fill the area, where ymax > ymin.
Does anybody know how this is done?
Thanks for your help.
See Question&Answers more detail:os