top of page

How to Create a Correlation Matrix in R

person typing on a computer

Overview

Correlation is a statistic that measures the degree to which two variables move in coordination with each other. Where, if the two variables move in the same direction, then those variables are said to have a positive correlation. If they move in opposite directions, then they are said to have a negative correlation. The correlation coefficient is always represented as a value between -1 and +1, which provides key insights into how variables interact: +1 (perfect positive correlation) – indicates that variables change in the same direction, 0 - indicates that there is no linear relationship between the variables, -1 (perfect negative correlation) - indicates that variables change in opposite directions.

Correlation matrix on the other hand is a statistical tool used to evaluate and visualize the relationships between multiple variables in a dataset. It is a table that displays correlation coefficients, which measure the strength and direction of relationships between different variables.

Example of a correlation matrix.

sample correlation matrix

In this article, I will show you how to calculate and visualize correlation matrix using R.

Step-by-Step Guide

To create a correlation matrix in R, load the necessary packages in your R session as shown below.

loading packages in r session

Set your working directory and load your data.

setting working directory and loading dataset

Here is the snapshot of the data used in this example.

sample data

Compute the correlation matrix as shown below.

computing correlation matrix

See the computed correlation matrix.

sample correlation matrix

Option 1: Plotting the correlation matrix (using corrplot package) using the code below.

plotting correlation matrix in r

Executing the above code generates the view below.

sample correlation matrix

Note other visual elements that you can use with the method argument are pie, color, and number – while the type argument can be used with upper and lower to achieve different shapes of correlation matrices as shown below.

e.g. using Method = “color” & type = “upper”

plotting correlation matrix in r

Executing the above code generates the view below.

sample correlation matrix

E.g. using Method = “number” & type = “lower”

plotting correlation matrix in r

Executing the above code generates the view below.

sample correlation matrix

Option 2: Plotting correlation matrix (using ggcorrplot package) using the code below.

plotting correlation matrix in r with ggcorrplot package

Executing the above code generates the view below.

sample correlation matrix

Note other visual elements that you can use with the method argument are circle and square – while the type argument can be used with upper and lower to achieve different shapes of correlation matrices as shown below.

E.g. using Method = “square” & type = “upper”

plotting correlation matrix in r with ggcorrplot package

Executing the above code returns the view below.

sample correlation matrix

E.g. using Method = “circle” & type = “lower”

plotting correlation matrix in r with ggcorrplot package

Executing the above code returns the view below.

sample correlation matrix

Conclusion

Correlation matrices are powerful statistical tools that provide comprehensive insights into the relationships between multiple variables in a dataset. By utilizing R's built-in functions like cor(), researchers and data analysts can efficiently compute and visualize complex interdependencies between variables - transforming raw data into meaningful insights, supporting more robust statistical analysis and decision-making processes.

If you like the work we do and would like to work with us, drop us an email on our contacts page and we’ll reach out!

Thank you for reading!

Original.png

We Support You Deliver Business-Focused Solutions That Enable Data-Driven Decision Making.

  • Tableau profile
  • YouTube
  • White LinkedIn Icon
  • Facebook
  • X

QUICK LINKS

CONTACT US

+254 738 307 495

East Gate Mall, Donholm

3rd Floor Suite No. 3i

Nairobi, Kenya

Join our mailing list

bottom of page