You can use the corrplot function from the corrplot package in R to create a correlation matrix for a data frame.
This function offers a massive variety of arguments you can use to customize the correlation matrix, but here are some of the most common ways to use it:
Method 1: Create Correlation Matrix with Circles Inside Matrix
library(corrplot) #create correlation matrix with circles shown inside matrix corrplot(cor(df))
Method 2: Create Correlation Matrix with Variables in Alphabetical Order
library(corrplot) #create correlation matrix with variables in alphabetical order corrplot(cor(df), order='alphabet')
Method 3: Create Correlation Matrix with Coefficients Inside Matrix
library(corrplot) #create correlation matrix with correlation coefficients shown inside matrix corrplot(cor(df), method='number')
Method 4: Create Correlation Matrix with Shaded Cells Inside Matrix
library(corrplot) #create correlation matrix with shaded cells inside matrix corrplot(cor(df), method='color')
The following examples show how to use each method in practice with the following data frame in R that contains information about various basketball players:
#create data frame
df <- data.frame(assists=c(4, 5, 5, 6, 7, 8, 8, 10),
rebounds=c(12, 14, 13, 7, 8, 8, 9, 13),
points=c(22, 24, 26, 26, 29, 32, 20, 14),
steals=c(5, 6, 7, 7, 8, 5, 3, 4))
#view data frame
df
assists rebounds points steals
1 4 12 22 5
2 5 14 24 6
3 5 13 26 7
4 6 7 26 7
5 7 8 29 8
6 8 8 32 5
7 8 9 20 3
8 10 13 14 4
Example 1: Create Correlation Matrix with Circles Inside Matrix
We can use the following syntax to create a correlation matrix that contains circles inside the matrix in which the colors indicate the sign of the correlation coefficient and the size represents the strength of the correlation:
library(corrplot)
#create correlation matrix with circles shown inside matrix
corrplot(cor(df))

Example 2: Create Correlation Matrix with Variables in Alphabetical Order
We can use the order argument to specify that we’d like to create a correlation matrix with the variables in alphabetical order:
library(corrplot)
#create correlation matrix with variables in alphabetical order
corrplot(cor(df), order='alphabet')

Example 3: Create Correlation Matrix with Coefficients Inside Matrix
We can use the method argument to specify that we’d like to create a correlation matrix with the correlation coefficients displayed inside the matrix:
library(corrplot)
#create correlation matrix with correlation coefficients shown inside matrix
corrplot(cor(df), method='number')

Example 4: Create Correlation Matrix with Shaded Cells Inside Matrix
We can use the method argument to specify that we’d like to create a correlation matrix with shaded cells displayed inside the matrix:
library(corrplot)
#create correlation matrix with shaded cells inside matrix
corrplot(cor(df), method='color')

Additional Resources
The following tutorials explain how to perform other common operations in R:
How to Calculate Rolling Correlation in R
How to Calculate Spearman Rank Correlation in R
How to Calculate Correlation in R with Missing Values