My data is like below. Two column, serial number (SL) and the expression value (log)
> df
SL log
1 1.5
2 -2.5
3 1.0
4 2.5
5 -1.
> ggplot(df, aes(x = SL, y = log)) +
geom_point(size = 0.5, alpha = 0.6, shape = 19, color = "gray") +
geom_smooth(method = "loess", se = FALSE, linewidth = 0.5, span = 0.09) +
geom_hline(yintercept = 0, color = "black", lwd = 0.5)
However, I want to fill the geome_smooth loess line to be filled with red color if it is above the Y=0 intercept line and green color if below Y=0 intercept line. Example figure below.
How can I do that?






I don't think there's a super simple answer, since you first need to extract the loess line to use it with geom_area (which can't vary in fill) or geom_ribbon (which won't automatically separate regions of the same sign).
I like using
ggbraidonce we've extracted the loess line, as it will make a nice clean interpolation with separate fill regions.Given some fake data:
We could make a plot and extract the loess curve using the approach here:
Then we have a slightly tricky problem involving interpolation, as detailed here and here. One simple solution is to use the
ggbraidpackage, which identifies the exact crossing points and cleanly separates the fill areas.