You are on page 1of 4

11/14/2019 How to Create a Correlation Matrix in R | Displayr

R HOW TO...

How to Create a
Correlation Matrix
in R

by Chris Facer

A correlation matrix is a table of correlation coefficients for a set of variables used


to determine if a relationship exists between the variables. The coefficient
indicates both the strength of the relationship as well as the direction (positive
vs. negative correlations). In this post I show you how to calculate and visualize
a correlation matrix using R.

As an example, let’s look at a technology survey in which respondents were asked which devices they
owned. We want to examine if there is a relationship between any of the devices owned by running a
correlation matrix for the device ownership variables. To do this in R, we first load the data into our session
using the read.csv function:

1 mydata = read.csv("https://wiki.q-researchsoftware.com/images/b/b9/Ownership.csv"

The cor function

The simplest and most straight-forward to run a correlation in R is with the cor function:

1 mydata.cor = cor(mydata)

This returns a simple correlation matrix showing the correlations between pairs of variables (devices).

https://www.displayr.com/how-to-create-a-correlation-matrix-in-r/?utm_referrer=https%3A%2F%2Fwww.google.com%2F 1/4
11/14/2019 How to Create a Correlation Matrix in R | Displayr

You can choose the correlation coefficient to be computed using the method parameter. The default
method is Pearson, but you can also compute Spearman or Kendall coefficients.

1 mydata.cor = cor(mydata, method = c("spearman"))

Significance levels (p-values) can also be generated using the rcorr function which is found in the Hmisc
package. First install the required package and load the library.

1 install.packages("Hmisc")
2 library("Hmisc")

Use the following code to run the correlation matrix with p-values. Note that the data has to be fed to the
rcorr function as a matrix.

1 mydata.rcorr = rcorr(as.matrix(mydata))
2 mydata.rcorr

This generates one table of correlation coefficients (the correlation matrix) and another table of the p-
values. By default, the correlations and p-values are stored in an object of class type rcorr. To extract the
values from this object into a useable data structure, you can use the following syntax:

1 mydata.coeff = mydata.rcorr$r
2 mydata.p = mydata.rcorr$P

Objects of class type matrix are generated containing the correlation coefficients and p-values.

Visualizing the correlation matrix

There are several packages available for visualizing a correlation matrix in R. One of the most common is
the corrplot function. We first need to install the corrplot package and load the library.

https://www.displayr.com/how-to-create-a-correlation-matrix-in-r/?utm_referrer=https%3A%2F%2Fwww.google.com%2F 2/4
11/14/2019 How to Create a Correlation Matrix in R | Displayr

1 install.packages("corrplot")
2 library(corrplot)

Next, we’ll run the corrplot function providing our original correlation matrix as the data input to the
function.

1 corrplot(mydata.cor)

A default correlation matrix plot (called a Correlogram) is generated. Positive correlations are displayed in
a blue scale while negative correlations are displayed in a red scale.

We can also generate a Heatmap object again using our correlation coefficients as input to the Heatmap.
Because the default Heatmap color scheme is quite unsightly, we can first specify a color palette to use in
the Heatmap. The value at the end of the function specifies the amount of variation in the color scale.
Typically no more than 20 is needed here. We then use the heatmap function to create the output:

1 palette = colorRampPalette(c("green", "white", "red")) (20)


2 heatmap(x = mydata.cor, col = palette, symm = TRUE)

https://www.displayr.com/how-to-create-a-correlation-matrix-in-r/?utm_referrer=https%3A%2F%2Fwww.google.com%2F 3/4
11/14/2019 How to Create a Correlation Matrix in R | Displayr

Create your own correlation matrix

https://www.displayr.com/how-to-create-a-correlation-matrix-in-r/?utm_referrer=https%3A%2F%2Fwww.google.com%2F 4/4

You might also like