# How to Create a Correlation Matrix in R

**A c orrelation 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:

[sourcecode language="r"]

mydata = read.csv("https://wiki.q-researchsoftware.com/images/b/b9/Ownership.csv", header = TRUE, fileEncoding="latin1")

[/sourcecode]

Create your own correlation matrix

## The cor function

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

[sourcecode language="r"]

mydata.cor = cor(mydata)

[/sourcecode]

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

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.

[sourcecode language="r"]

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

[/sourcecode]

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.

[sourcecode language="r"]

install.packages("Hmisc")

library("Hmisc")

[/sourcecode]

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.

[sourcecode language="r"]

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

mydata.rcorr

[/sourcecode]

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:

[sourcecode language="r"]

mydata.coeff = mydata.rcorr$r

mydata.p = mydata.rcorr$P

[/sourcecode]

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

Create your own correlation matrix

## 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.

[sourcecode language="r"]

install.packages("corrplot")

library(corrplot)

[/sourcecode]

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

[sourcecode language="r"]

corrplot(mydata.cor)

[/sourcecode]

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:

[sourcecode language="r"]

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

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

[/sourcecode]

Create your own correlation matrix in Displayr. Sign up below to get started.

Sign up for free