Open In App

How to Make a Scatter Plot Matrix in R

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A scatterplot matrix is ​​a grid of scatterplots that allows us to see how different pairs of variables are related to each other. We can easily generate a scatterplot matrix using the pairs() function in R programming. In this article, we will walk through the process of creating a scatterplot matrix in R step by step using different packages.

Before we start, make sure you have R and R Studio installed on your computer, which contains the following R packages: ggplot2 , GGally, and Psych. If you don’t have these packages installed, you can install them by running the following code in the console of R Studio:

install.packages("ggplot2")
install.packages("GGally")
install.packages("psych")

How to make a Scatterplot Matrix in R using pairs() function?

The pairs() function is a function that is used to create a scatterplot matrix for a given set of variables of a dataset. It is a useful function that helps in examining the relationship between multiple variables in a dataset. It creates a scatterplot matrix with variables plotted against every other variable in the dataset.

Here are some step which help you in creating scatter plot matrix in R using pairs() function:

Step 1: Load the dataset

We need to load the data for which we want to create a scatterplot matrix. The data() function allows us to load data. In this example, we’ll use R built-in dataset mtcars that contains information about 32 different car models. The dataset contains 11 variables, such as miles per gallon (mpg), number of cylinders, horsepower, weight, acceleration, and many more.

data(mtcars)

Step 2: Create a scatterplot matrix using the pairs() function

pairs(mtcars[, c(1, 3:6)], main = "Scatter Plot Matrix for mtcars Dataset")

Some of the parameters which we can use to our plot more interactive graphs are as follows:

  1. diag.panel: This is a parameter of the pairs() function, which is used to create a histogram or density plot for each variable in the diagonal part of the matrix.
  2. text.panel: This is a parameter of the pairs() function, which is used to add text to the matrix.
  3. scale: This is a parameter of the pairs() function, which is used to adjust the size of the plot.
  4. font.labels: This is a parameter of the pairs() function, which specifies the font size of the label.

Complete Code for creating Scatter Plot Matrix using Pairs() Function:

R




data(mtcars)
# Create a scatter plot matrix using the pairs() function
pairs(mtcars[, c(1, 3:6)], main = "Scatter Plot Matrix for mtcars Dataset")


Output:

Scatterplot Matrix using pair() function

How to Make a Scatterplot Matrix in R using GGally Package?

In the GGally package we have a ggpairs() function which we can use to create a matrix of scatterplots for a given set of variables of a dataset to examine pairwise relationships between variables in a dataset. It creates a scatterplot matrix with variables plotted against every other variable in the dataset.

Here are some steps which will help you in creating a scatter plot matrix in R using GGally Package:

Step 1: Importing Required Packages

library(ggplot2)
library(GGally)

Step 2: Load the dataset

Next, we need to load the data for which we want to create a scatterplot matrix. In this example, we’ll use R built-in dataset mtcars, which is a dataset of cars, using the data() method.

data(mtcars)

Step 3: Create Scatterplot using ggpairs() method

We’ll use the ggpairs() function from the GGally package to make a scatter plot matrix. This function accepts a dataset along with a number of parameters that determine how the plot will look.

Scatter_Matrix <- ggpairs(mtcars,columns = c(1, 3:6),
    title = "Scatter Plot Matrix for mtcars Dataset", axisLabels = "show")

Some of the parameters which we can use to our plot a more interactive graph are as follows:

  1. diag: It is used for the diagonal of the matrix of plots for specific requirements. For example ‘diag=”density”‘: which adds a density plot to the diagonal of each plot. 
  2. upper: It is used for applying the specific requirements to the upper triangle of the matrix of plots.
  3. lower: It is used for applying the specific requirements to the lower triangle of the matrix of plots.

Step 4: Save the Scatterplot (optional)

The ggsave() method can be used to save our scatter plot matrix as an image file once we are satisfied with our scatter plot matrix.

ggsave("scatter plot matrix.png", Scatter_Matrix, width = 7, height = 7, units = "in")

The complete Code for creating Scatterplot Matrix Using GGally Package is as follows:

R




library(ggplot2)
library(GGally)
data(mtcars)
Scatter_Matrix <- ggpairs(mtcars,columns = c(1, 3:6),
                          title = "Scatter Plot Matrix for mtcars Dataset",
                          axisLabels = "show")
ggsave("Scatter plot matrix.png", Scatter_Matrix, width = 7,
       height = 7, units = "in")
Scatter_Matrix


Output:

Scatterplot using ggpairs() function

How to Make a Scatterplot Matrix in R using Psych Package?

In the Psych package, we have a pairs.panel() function which is used to create a matrix of scatterplots for a given set of variables of a dataset to examine pairwise relationships between variables in a dataset. It creates a scatterplot matrix with variables plotted against every other variable in the dataset and it also has correlation coefficients for each pair of variables of a dataset.

Here are some steps that will help you create a scatter plot matrix in R using Psych Package:

Step 1: Importing Required Packages

library(ggplot2)
library(psych)

Step 2:  Load the dataset

Next, we need to load the data for which we want to create a scatterplot matrix.

data(mtcars)

Step 3: Create Scatterplot using pairs.panels() function

We’ll use the pairs.panels() function from the psych package to make a scatter plot matrix. This function accepts a dataset along with a number of parameters that determine how the plot will look.

Scatter_Matrix <- pairs.panels(mtcars[, c(1, 3:6)], main = "Scatter Plot Matrix for mtcars Dataset")

Some of the parameters which we can use to our plot more interactive:

  1. col: It is used for specifying the color of the points in the plot.
  2. bg: It is used for specifying the background color of the plot.
  3. pch: It is used for specifying the type of symbol to be used in the plot.
  4. labels: It is used for specifying the labels for the variables in a given dataset.

Step 4: Save the Scatterplot (optional)

(optional)The ggsave() method can be used to save our scatter plot matrix as an image file once we are satisfied with our scatter plot matrix.

ggsave("scatter plot matrix.png", Scatter_Matrix, width = 7, height = 7, units = "in")

Complete Code for creating Scatter Plot Matrix using Psych Package

R




library(ggplot2)
library(psych)
data(mtcars)
Scatter_Matrix <- pairs.panels(mtcars[, c(1, 3:6)], main = "Scatter Plot Matrix for mtcars Dataset")
ggsave("scatter plot matrix.png", Scatter_Matrix, width = 7, height = 7, units = "in")


Output:

Scatterplot using Psych Package



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads