Skip to content
Prev 386099 / 398513 Next

How to shade area between lines in ggplot2

Hi

Put fill outside aes

p+geom_ribbon(aes(ymin = slope_1*x + intercept_1 - 1/w[2],
ymax = slope_1*x + intercept_1 + 1/w[2]), fill = "blue", alpha=0.1)

The "hole" is because you have two levels of data (red and blue). To get rid 
of this you should put new data in ribbon call.
Something like

newdat <- trainset
newdat$z <- factor(0)
p+geom_ribbon(data=newdat, aes(ymin = slope_1*x + intercept_1 - 1/w[2],
ymax = slope_1*x + intercept_1 + 1/w[2]), fill = "yellow", alpha=0.1)

Cheers
Petr