How do I plot a classification graph of a SVM in R
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
To plot an SVM classification graph in R, use the plot() method from the e1071 package on an svm object. This displays the decision boundary and support vectors for two-feature classification. For more customizable plots, use ggplot2 with a grid of predicted values to visualize the decision regions. Both approaches require the data to have exactly two features (or you must select two features to plot).
Basic SVM Plot with e1071
The plot() method shows:
- Colored regions for each class's decision area
- Data points marked by class
- Support vectors highlighted with crosses
Customizing the e1071 Plot
ggplot2 Visualization (More Control)
Highlighting Support Vectors
Different Kernel Comparison
Multi-Class SVM Plot
Plotting with Decision Values (Margins)
The contour lines at z = -1, z = 0, and z = 1 show the decision boundary and the margin boundaries.
Common Pitfalls
- More than 2 features: SVM decision boundaries in 2D can only be plotted for 2 features. If your model uses more features, select the 2 most important ones for visualization or use dimensionality reduction (PCA) to project to 2D before plotting.
- Grid resolution too low: Using too few grid points (e.g.,
length.out = 50) makes the decision boundary look pixelated. Use at least 100-200 points per axis for smooth boundaries, but balance against computation time. - Factor levels not dropped: After subsetting data (e.g., removing "setosa"), unused factor levels remain. Use
droplevels()to remove them, otherwise the SVM trains with phantom classes. - Kernel mismatch with data: A linear kernel cannot capture non-linear boundaries. If the classes overlap in complex ways, try
kernel = "radial". If the plot shows poor separation, tune thecostandgammaparameters. - Forgetting to scale features: SVM is sensitive to feature scales. If one feature ranges 0-1 and another ranges 0-1000, the SVM is dominated by the larger feature. Use
scale = TRUE(the default ine1071::svm) to standardize features before training.
Summary
- Use
plot(model, data, Feature1 ~ Feature2)frome1071for quick SVM visualization - Use
ggplot2with a prediction grid for customizable decision boundary plots - Highlight support vectors by extracting
model$indexand plotting them differently - Compare kernels by training multiple models and arranging plots with
gridExtra - Only 2 features can be visualized directly — use PCA for higher-dimensional data
Related reading
- How do I predict new data's cluster after clustering training data?
- How do I print inside the loss function during training in Keras?
- How do I print the model summary in PyTorch?
- How do I profile a tf.data.Dataset?
- How do I plot in real-time in a while loop?
- How do I print the full NumPy array, without truncation?
- How do I prepend to a short python list?
- How do I prevent malicious DHT clients that might want to alter/delete my DHT data?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.