This post explains how to build a scatterplot matrix with the car package. It provides several reproducible examples with explanation and R code.
plot()
function
This is a scatterplot matrix built with the
scatterplotMatrix()
function of the
car
package.
See more correlogram examples in the dedicated section.
Note the |cyl
syntax: it means that categories
available in the cyl
variable must be represented
distinctly (color, shape, size..).
# Packages
library(car)
library(RColorBrewer) # for the color palette
# Let's use the car dataset natively available in R
data <- mtcars
# Make the plot
my_colors <- brewer.pal(nlevels(as.factor(data$cyl)), "Set2")
scatterplotMatrix(~mpg+disp+drat|cyl, data=data ,
reg.line="" , smoother="", col=my_colors ,
smoother.args=list(col="grey") , cex=1.5 ,
pch=c(15,16,17) ,
main="Scatter plot with Three Cylinder Options"
)
👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥