Drawing scatterplot matrices

R
graphs
correlational studies
non-linearities
multiple regression
Author

Jan Vanhove

Published

November 28, 2019

This is just a quick blog post to share a function with which you can draw scatterplot matrices.

Scatterplot matrices are useful for displaying the intercorrelation between several continuous variables. Based on the help page for the pairs() function in R, I put together a function for quickly drawing scatterplot matrices that also show the Pearson correlation coefficients for the bivariate relationships as well as the number of observations that go into them: scatterplot_matrix(). Here’s how you’d use it:

# Read in the function
source("https://janhove.github.io/RCode/scatterplot_matrix.R")

# Example: airquality is a built-in dataset in R
data(airquality)

scatterplot_matrix(
  x = subset(airquality, select = c(Ozone, Wind, Temp, Solar.R, Month)),
  labels = c("Ozone", "Wind", "Temperature", "Solar radiation", "Month")
)

Use:

Output:

Functions you can use instead of this one are pairscor.fnc() in the languageR package, and ggscatmat() and ggpairs() in the GGally package.