Skip to content
Prev 386083 / 398513 Next

How to shade area between lines in ggplot2

Hello,

What about this?


straightline <- function(x, slope, intercept) slope*x + intercept

p <- ggplot(data = trainset, aes(x=x, y=y, color=z)) +
   geom_point() +
   geom_ribbon(aes(ymin = straightline(x, slope_1, intercept_1 + 1/w[2]),
                   ymax = straightline(x, slope_1, intercept_1 - 1/w[2])),
               color = NA, fill = "grey", alpha = 0.5) +
   geom_abline(slope = slope_1, intercept = intercept_1) +
   scale_color_manual(values = c("red", "blue"))

p


Note that now the middle line is drawn last. Otherwise it will be greyed 
by the ribbon.

Hope this helps,

Rui Barradas

?s 08:58 de 23/10/20, Luigi Marongiu escreveu: