Get startedGet started for free

Visualizing support vectors using ggplot

In this exercise you will plot the training dataset you used to build a linear SVM and mark out the support vectors. The training dataset has been preloaded for you in the dataframe trainset and the SVM model is stored in the variable svm_model.

This exercise is part of the course

Support Vector Machines in R

View Course

Exercise instructions

  • Load ggplot2.
  • Plot the training dataset.
  • Mark out the support vectors on the plot using their indices from the SVM model.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

#load ggplot
library(ggplot2)

#build scatter plot of training dataset
scatter_plot <- ggplot(data = ___, aes(x = x1, y = x2, color = y)) + 
    geom_point() + 
    scale_color_manual(values = c("red", "blue"))
 
#add plot layer marking out the support vectors 
layered_plot <- 
    scatter_plot + geom_point(data = trainset[svm_model$___, ], aes(x = x1, y = x2), color = "purple", size = 4, alpha = 0.5)

#display plot
layered_plot
Edit and Run Code