You are on page 1of 2

11/23/2018 RPubs - Correlation Plots Using "corrplot" Package

RPubs brought to you by RStudio


Correlation Plots Using "corrplot" Package by melike Last updated over 2 years ago
Sign in Register

Correlation Plots Using corrplot Package Comments (–) Share Hide Toolbars

melike
Let’s say we have a 25-column matrix and we want to analyse and visualize the correlation between columns. There
are several possible ways. This post is about using the corrplot package.

First, let’s look at the object briefly:

mat[1:5,1:5]

## x_1 x_2 x_3 x_4 x_5


## [1,] -19.56761 -19.464597 -29.44343 24.298796 -25.57141
## [2,] -22.35271 -25.073553 -19.20713 16.564984 -23.71568
## [3,] -23.73841 -10.949853 -24.10458 9.953472 -22.21538
## [4,] -18.36704 -14.156476 -17.70389 19.239598 -15.83134
## [5,] -13.00010 -8.743272 -11.18862 15.272035 -14.60418

dim(mat)

## [1] 100 25

class(mat)

## [1] "matrix"

The first step is calculating the correlations. Note that I use Spearman’s Rank Correlation Coefficients. This is just
because I use a simulated data and not quite sure if there are outliers.

corr_mat=cor(mat,method="s")
corr_mat[1:5,1:5]

## x_1 x_2 x_3 x_4 x_5


## x_1 1.00000000 0.22557456 -0.05596160 -0.02371437 0.57196520
## x_2 0.22557456 1.00000000 -0.06113411 0.04896490 0.38661866
## x_3 -0.05596160 -0.06113411 1.00000000 -0.35621962 0.04523252
## x_4 -0.02371437 0.04896490 -0.35621962 1.00000000 -0.12919292
## x_5 0.57196520 0.38661866 0.04523252 -0.12919292 1.00000000

Manual inspection of a 25x25 matrix is time-consuming and uninformative. Thus, we need a better way to visualize
the results.

library(corrplot)
corrplot(corr_mat)

https://rpubs.com/melike/corrplot 1/2
11/23/2018 RPubs - Correlation Plots Using "corrplot" Package

https://rpubs.com/melike/corrplot 2/2

You might also like