![person typing on a computer](https://static.wixstatic.com/media/e16c6a_980d6b79edb24561ae634662c32cdfb7~mv2.jpg/v1/fill/w_980,h_513,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/e16c6a_980d6b79edb24561ae634662c32cdfb7~mv2.jpg)
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](https://static.wixstatic.com/media/e16c6a_d762d450ced34f3ba901d8ad36832c90~mv2.jpeg/v1/fill/w_800,h_311,al_c,q_80,enc_auto/e16c6a_d762d450ced34f3ba901d8ad36832c90~mv2.jpeg)
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](https://static.wixstatic.com/media/e16c6a_b083ca2ee04b4460a0fb9a379dfcb812~mv2.jpg/v1/fill/w_946,h_176,al_c,q_80,enc_auto/e16c6a_b083ca2ee04b4460a0fb9a379dfcb812~mv2.jpg)
Set your working directory and load your data.
![setting working directory and loading dataset](https://static.wixstatic.com/media/e16c6a_4138db983f464fd28fcf3c10d3a76de5~mv2.jpg/v1/fill/w_899,h_124,al_c,q_80,enc_auto/e16c6a_4138db983f464fd28fcf3c10d3a76de5~mv2.jpg)
Here is the snapshot of the data used in this example.
![sample data](https://static.wixstatic.com/media/e16c6a_59449ade29ee43948a28d8852b3b7ac0~mv2.jpg/v1/fill/w_960,h_383,al_c,q_85,enc_auto/e16c6a_59449ade29ee43948a28d8852b3b7ac0~mv2.jpg)
Compute the correlation matrix as shown below.
![computing correlation matrix](https://static.wixstatic.com/media/e16c6a_28a104220110455a96698064ded90512~mv2.jpg/v1/fill/w_900,h_100,al_c,q_80,enc_auto/e16c6a_28a104220110455a96698064ded90512~mv2.jpg)
See the computed correlation matrix.
![sample correlation matrix](https://static.wixstatic.com/media/e16c6a_04730be50eb44ce4b3cfcad81995855c~mv2.jpg/v1/fill/w_980,h_166,al_c,q_80,usm_0.66_1.00_0.01,enc_auto/e16c6a_04730be50eb44ce4b3cfcad81995855c~mv2.jpg)
Option 1: Plotting the correlation matrix (using corrplot package) using the code below.
![plotting correlation matrix in r](https://static.wixstatic.com/media/e16c6a_9d99c2718bfd44db9a35f9c018c12307~mv2.jpg/v1/fill/w_856,h_102,al_c,q_80,enc_auto/e16c6a_9d99c2718bfd44db9a35f9c018c12307~mv2.jpg)
Executing the above code generates the view below.
![sample correlation matrix](https://static.wixstatic.com/media/e16c6a_97c88108557c4e35895ee3b0638dfc5d~mv2.jpeg/v1/fill/w_657,h_430,al_c,q_80,enc_auto/e16c6a_97c88108557c4e35895ee3b0638dfc5d~mv2.jpeg)
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](https://static.wixstatic.com/media/e16c6a_d47e8af94a0f413893b5986616ae77c4~mv2.jpg/v1/fill/w_866,h_101,al_c,q_80,enc_auto/e16c6a_d47e8af94a0f413893b5986616ae77c4~mv2.jpg)
Executing the above code generates the view below.
![sample correlation matrix](https://static.wixstatic.com/media/e16c6a_6bfd4bb4c9404a24a0637dbafbfd09a5~mv2.jpeg/v1/fill/w_657,h_430,al_c,q_80,enc_auto/e16c6a_6bfd4bb4c9404a24a0637dbafbfd09a5~mv2.jpeg)
E.g. using Method = “number” & type = “lower”
![plotting correlation matrix in r](https://static.wixstatic.com/media/e16c6a_9bba45b21f984f42bcde25d25d80110d~mv2.jpg/v1/fill/w_856,h_99,al_c,q_80,enc_auto/e16c6a_9bba45b21f984f42bcde25d25d80110d~mv2.jpg)
Executing the above code generates the view below.
![sample correlation matrix](https://static.wixstatic.com/media/e16c6a_b1703ff206e34f92bfc6e4180fabc378~mv2.jpeg/v1/fill/w_657,h_430,al_c,q_80,enc_auto/e16c6a_b1703ff206e34f92bfc6e4180fabc378~mv2.jpeg)
Option 2: Plotting correlation matrix (using ggcorrplot package) using the code below.
![plotting correlation matrix in r with ggcorrplot package](https://static.wixstatic.com/media/e16c6a_f9d2a0f5c8da4fe489cbfd57ddd42b2f~mv2.jpg/v1/fill/w_980,h_97,al_c,q_80,usm_0.66_1.00_0.01,enc_auto/e16c6a_f9d2a0f5c8da4fe489cbfd57ddd42b2f~mv2.jpg)
Executing the above code generates the view below.
![sample correlation matrix](https://static.wixstatic.com/media/e16c6a_d632ed57cd6c4379ad3f64f05ac7542b~mv2.jpeg/v1/fill/w_724,h_430,al_c,q_80,enc_auto/e16c6a_d632ed57cd6c4379ad3f64f05ac7542b~mv2.jpeg)
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](https://static.wixstatic.com/media/e16c6a_fa462d91421c46dd96d848888e3669b7~mv2.jpg/v1/fill/w_980,h_95,al_c,q_80,usm_0.66_1.00_0.01,enc_auto/e16c6a_fa462d91421c46dd96d848888e3669b7~mv2.jpg)
Executing the above code returns the view below.
![sample correlation matrix](https://static.wixstatic.com/media/e16c6a_d5e76d28446843ebaced4b1863163269~mv2.jpeg/v1/fill/w_706,h_430,al_c,q_80,enc_auto/e16c6a_d5e76d28446843ebaced4b1863163269~mv2.jpeg)
E.g. using Method = “circle” & type = “lower”
![plotting correlation matrix in r with ggcorrplot package](https://static.wixstatic.com/media/e16c6a_4c31a48699c44ee785dbeb82f4b70c04~mv2.jpg/v1/fill/w_980,h_96,al_c,q_80,usm_0.66_1.00_0.01,enc_auto/e16c6a_4c31a48699c44ee785dbeb82f4b70c04~mv2.jpg)
Executing the above code returns the view below.
![sample correlation matrix](https://static.wixstatic.com/media/e16c6a_af943ffeeb374195a2dc43b037d8a8ad~mv2.jpeg/v1/fill/w_719,h_430,al_c,q_80,enc_auto/e16c6a_af943ffeeb374195a2dc43b037d8a8ad~mv2.jpeg)
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!