Hello, I am running SVM and showing the results with ggplot2. The results include the decision boundaries, which are two dashed lines parallel to a solid line. I would like to remove the dashed lines and use a shaded area instead. How can I do that? Here is the code I wrote.. ``` library(e1071) library(ggplot2) set.seed(100) x1 = rnorm(100, mean = 0.2, sd = 0.1) y1 = rnorm(100, mean = 0.7, sd = 0.1) y2 = rnorm(100, mean = 0.2, sd = 0.1) x2 = rnorm(100, mean = 0.75, sd = 0.1) df = data.frame(x = c(x1,x2), y=c(y1,y2), z=c(rep(0, length(x1)), rep(1, length(x2)))) df$z = factor(c(rep(0, length(x1)), rep(1, length(x2)))) df[, "train"] <- ifelse(runif(nrow(df)) < 0.8, 1, 0) trainset <- df[df$train == 1, ] testset <- df[df$train == 0, ] trainColNum <- grep("train", names(df)) trainset <- trainset[, -trainColNum] testset <- testset[, -trainColNum] head(trainset); str(df) svm_model<- svm(z ~ ., data = trainset, type = "C-classification", kernel = "linear", scale = FALSE) #! plot p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + geom_point() + scale_color_manual(values = c("red", "blue")) # show decision boundaries 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) ### here we go: can I use a shaded area between these two lines? ### p = p + geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], linetype = "dashed") + geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], linetype = "dashed") print(p) ``` Thank you -- Best regards, Luigi
Hi Did you try google? I got several answers using your question e.g. https://stackoverflow.com/questions/54687321/fill-area-between-lines-using-g gplot-in-r Cheers Petr> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Luigi Marongiu > Sent: Friday, October 23, 2020 9:59 AM > To: r-help <r-help at r-project.org> > Subject: [R] How to shade area between lines in ggplot2 > > Hello, > I am running SVM and showing the results with ggplot2. The results include > the decision boundaries, which are two dashed lines parallel to a solidline. I> would like to remove the dashed lines and use a shaded area instead. How > can I do that? > Here is the code I wrote.. > ``` > library(e1071) > library(ggplot2) > > set.seed(100) > x1 = rnorm(100, mean = 0.2, sd = 0.1) > y1 = rnorm(100, mean = 0.7, sd = 0.1) > y2 = rnorm(100, mean = 0.2, sd = 0.1) > x2 = rnorm(100, mean = 0.75, sd = 0.1) > df = data.frame(x = c(x1,x2), y=c(y1,y2), > z=c(rep(0, length(x1)), rep(1, length(x2)))) df$z factor(c(rep(0, > length(x1)), rep(1, length(x2)))) df[, "train"] <- ifelse(runif(nrow(df))< 0.8, 1, 0)> trainset <- df[df$train == 1, ] testset <- df[df$train == 0, ] trainColNum<-> grep("train", names(df)) trainset <- trainset[, -trainColNum] testset <-testset[,> -trainColNum] head(trainset); str(df) > > svm_model<- svm(z ~ ., > data = trainset, > type = "C-classification", > kernel = "linear", > scale = FALSE) > > #! plot > p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + > geom_point() + scale_color_manual(values = c("red", "blue")) # show > decision boundaries 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) ### here wego:> can I use a shaded area between these two lines? ### p = p + > geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], > linetype = "dashed") + > geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], > linetype = "dashed") > print(p) > > ``` > > Thank you > > -- > Best regards, > Luigi > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
I don't know if I can find a case similar to my need. I tried with: ``` p = p + geom_abline(slope = slope_1, intercept = intercept_1, col "royalblue4") p = p + geom_line() + geom_hline(yintercept = 5) + theme_classic() + geom_ribbon(aes(ymin=intercept_1 - 1/w[2],ymax=intercept_1 + 1/w[2]), fill="blue") ``` but did not work. Thanks anyway On Fri, Oct 23, 2020 at 10:26 AM PIKAL Petr <petr.pikal at precheza.cz> wrote:> > Hi > > Did you try google? I got several answers using your question > > e.g. > https://stackoverflow.com/questions/54687321/fill-area-between-lines-using-g > gplot-in-r > > Cheers > Petr > > > -----Original Message----- > > From: R-help <r-help-bounces at r-project.org> On Behalf Of Luigi Marongiu > > Sent: Friday, October 23, 2020 9:59 AM > > To: r-help <r-help at r-project.org> > > Subject: [R] How to shade area between lines in ggplot2 > > > > Hello, > > I am running SVM and showing the results with ggplot2. The results include > > the decision boundaries, which are two dashed lines parallel to a solid > line. I > > would like to remove the dashed lines and use a shaded area instead. How > > can I do that? > > Here is the code I wrote.. > > ``` > > library(e1071) > > library(ggplot2) > > > > set.seed(100) > > x1 = rnorm(100, mean = 0.2, sd = 0.1) > > y1 = rnorm(100, mean = 0.7, sd = 0.1) > > y2 = rnorm(100, mean = 0.2, sd = 0.1) > > x2 = rnorm(100, mean = 0.75, sd = 0.1) > > df = data.frame(x = c(x1,x2), y=c(y1,y2), > > z=c(rep(0, length(x1)), rep(1, length(x2)))) df$z > factor(c(rep(0, > > length(x1)), rep(1, length(x2)))) df[, "train"] <- ifelse(runif(nrow(df)) > < 0.8, 1, 0) > > trainset <- df[df$train == 1, ] testset <- df[df$train == 0, ] trainColNum > <- > > grep("train", names(df)) trainset <- trainset[, -trainColNum] testset <- > testset[, > > -trainColNum] head(trainset); str(df) > > > > svm_model<- svm(z ~ ., > > data = trainset, > > type = "C-classification", > > kernel = "linear", > > scale = FALSE) > > > > #! plot > > p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + > > geom_point() + scale_color_manual(values = c("red", "blue")) # show > > decision boundaries 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) ### here we > go: > > can I use a shaded area between these two lines? ### p = p + > > geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], > > linetype = "dashed") + > > geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], > > linetype = "dashed") > > print(p) > > > > ``` > > > > Thank you > > > > -- > > Best regards, > > Luigi > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting- > > guide.html > > and provide commented, minimal, self-contained, reproducible code.-- Best regards, Luigi
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:> > Hi > > Did you try google? I got several answers using your question > > e.g. > https://stackoverflow.com/questions/54687321/fill-area-between-lines-using-g > gplot-in-r > > Cheers > Petr > > > -----Original Message----- > > From: R-help <r-help-bounces at r-project.org> On Behalf Of Luigi Marongiu > > Sent: Friday, October 23, 2020 9:59 AM > > To: r-help <r-help at r-project.org> > > Subject: [R] How to shade area between lines in ggplot2 > > > > Hello, > > I am running SVM and showing the results with ggplot2. The results include > > the decision boundaries, which are two dashed lines parallel to a solid > line. I > > would like to remove the dashed lines and use a shaded area instead. How > > can I do that? > > Here is the code I wrote.. > > ``` > > library(e1071) > > library(ggplot2) > > > > set.seed(100) > > x1 = rnorm(100, mean = 0.2, sd = 0.1) > > y1 = rnorm(100, mean = 0.7, sd = 0.1) > > y2 = rnorm(100, mean = 0.2, sd = 0.1) > > x2 = rnorm(100, mean = 0.75, sd = 0.1) > > df = data.frame(x = c(x1,x2), y=c(y1,y2), > > z=c(rep(0, length(x1)), rep(1, length(x2)))) df$z > factor(c(rep(0, > > length(x1)), rep(1, length(x2)))) df[, "train"] <- ifelse(runif(nrow(df)) > < 0.8, 1, 0) > > trainset <- df[df$train == 1, ] testset <- df[df$train == 0, ] trainColNum > <- > > grep("train", names(df)) trainset <- trainset[, -trainColNum] testset <- > testset[, > > -trainColNum] head(trainset); str(df) > > > > svm_model<- svm(z ~ ., > > data = trainset, > > type = "C-classification", > > kernel = "linear", > > scale = FALSE) > > > > #! plot > > p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + > > geom_point() + scale_color_manual(values = c("red", "blue")) # show > > decision boundaries 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) ### here we > go: > > can I use a shaded area between these two lines? ### p = p + > > geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], > > linetype = "dashed") + > > geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], > > linetype = "dashed") > > print(p) > > > > ``` > > > > Thank you > > > > -- > > Best regards, > > Luigi > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting- > > guide.html > > and provide commented, minimal, self-contained, reproducible code.-- Best regards, Luigi
also from this site: https://plotly.com/ggplot2/geom_ribbon/ I get the answer is geom_ribbon but I am still missing something ``` #! 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], x=x, fill = "band"), alpha = 0.3) + scale_fill_manual("",values="grey12") ``` On Fri, Oct 23, 2020 at 10:26 AM PIKAL Petr <petr.pikal at precheza.cz> wrote:> > Hi > > Did you try google? I got several answers using your question > > e.g. > https://stackoverflow.com/questions/54687321/fill-area-between-lines-using-g > gplot-in-r > > Cheers > Petr > > > -----Original Message----- > > From: R-help <r-help-bounces at r-project.org> On Behalf Of Luigi Marongiu > > Sent: Friday, October 23, 2020 9:59 AM > > To: r-help <r-help at r-project.org> > > Subject: [R] How to shade area between lines in ggplot2 > > > > Hello, > > I am running SVM and showing the results with ggplot2. The results include > > the decision boundaries, which are two dashed lines parallel to a solid > line. I > > would like to remove the dashed lines and use a shaded area instead. How > > can I do that? > > Here is the code I wrote.. > > ``` > > library(e1071) > > library(ggplot2) > > > > set.seed(100) > > x1 = rnorm(100, mean = 0.2, sd = 0.1) > > y1 = rnorm(100, mean = 0.7, sd = 0.1) > > y2 = rnorm(100, mean = 0.2, sd = 0.1) > > x2 = rnorm(100, mean = 0.75, sd = 0.1) > > df = data.frame(x = c(x1,x2), y=c(y1,y2), > > z=c(rep(0, length(x1)), rep(1, length(x2)))) df$z > factor(c(rep(0, > > length(x1)), rep(1, length(x2)))) df[, "train"] <- ifelse(runif(nrow(df)) > < 0.8, 1, 0) > > trainset <- df[df$train == 1, ] testset <- df[df$train == 0, ] trainColNum > <- > > grep("train", names(df)) trainset <- trainset[, -trainColNum] testset <- > testset[, > > -trainColNum] head(trainset); str(df) > > > > svm_model<- svm(z ~ ., > > data = trainset, > > type = "C-classification", > > kernel = "linear", > > scale = FALSE) > > > > #! plot > > p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + > > geom_point() + scale_color_manual(values = c("red", "blue")) # show > > decision boundaries 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) ### here we > go: > > can I use a shaded area between these two lines? ### p = p + > > geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], > > linetype = "dashed") + > > geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], > > linetype = "dashed") > > print(p) > > > > ``` > > > > Thank you > > > > -- > > Best regards, > > Luigi > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting- > > guide.html > > and provide commented, minimal, self-contained, reproducible code.-- Best regards, Luigi
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:> Hello, > I am running SVM and showing the results with ggplot2. The results > include the decision boundaries, which are two dashed lines parallel > to a solid line. I would like to remove the dashed lines and use a > shaded area instead. How can I do that? > Here is the code I wrote.. > ``` > library(e1071) > library(ggplot2) > > set.seed(100) > x1 = rnorm(100, mean = 0.2, sd = 0.1) > y1 = rnorm(100, mean = 0.7, sd = 0.1) > y2 = rnorm(100, mean = 0.2, sd = 0.1) > x2 = rnorm(100, mean = 0.75, sd = 0.1) > df = data.frame(x = c(x1,x2), y=c(y1,y2), > z=c(rep(0, length(x1)), rep(1, length(x2)))) > df$z = factor(c(rep(0, length(x1)), rep(1, length(x2)))) > df[, "train"] <- ifelse(runif(nrow(df)) < 0.8, 1, 0) > trainset <- df[df$train == 1, ] > testset <- df[df$train == 0, ] > trainColNum <- grep("train", names(df)) > trainset <- trainset[, -trainColNum] > testset <- testset[, -trainColNum] > head(trainset); str(df) > > svm_model<- svm(z ~ ., > data = trainset, > type = "C-classification", > kernel = "linear", > scale = FALSE) > > #! plot > p = ggplot(data = trainset, aes(x=x, y=y, color=z)) + > geom_point() + scale_color_manual(values = c("red", "blue")) > # show decision boundaries > 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) > ### here we go: can I use a shaded area between these two lines? ### > p = p + geom_abline(slope = slope_1, intercept = intercept_1 - 1/w[2], > linetype = "dashed") + > geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2], > linetype = "dashed") > print(p) > > ``` > > Thank you >