Skip to content
Prev 386073 / 398513 Next

How to shade area between lines in ggplot2

I tried from this website
https://community.rstudio.com/t/fill-area-between-lines-using-ggplot-in-r/35355/2
it looked promising but did not work for me:
```
#! plot
p = ggplot(data = trainset, aes(x=x, y=y, color=z)) +
  geom_point() + scale_color_manual(values = c("red", "blue"))
# show support vectors
df_sv = trainset[svm_model$index, ]
p = p + geom_point(data = df_sv, aes(x=x, y=y),
                   color="purple", size=4, alpha=0.5)
# show hyperplane (decision boundaries are off set by 1/w[2])
w = t(svm_model$coefs) %*% svm_model$SV  # %*% = matrix multiplication
slope_1 = -w[1]/w[2]
intercept_1 = svm_model$rho / w[2]
p = p + geom_abline(slope = slope_1, intercept = intercept_1, col =
"royalblue4")
p = p + geom_ribbon(aes(ymin = intercept_1 - 1/w[2],
                        ymax = intercept_1 + 1/w[2]), fill = "grey70")
```
On Fri, Oct 23, 2020 at 10:26 AM PIKAL Petr <petr.pikal at precheza.cz> wrote: