Introduction

R package corrplot provides a visual exploratory tool on correlation matrix that supports automatic variable reordering to help detect hidden patterns among variables.

corrplot is very easy to use and provides a rich array of plotting options in visualization method, graphic layout, color, legend, text labels, etc. It also provides p-values and confidence intervals to help users determine the statistical significance of the correlations.

corrplot() has about 50 parameters, however the mostly common ones are only a few. We can get a correlation matrix plot with only one line of code in most scenes.

The mostly using parameters include method, type, order, diag, and etc.

There are seven visualization methods (parameter method) in corrplot package, named 'circle', 'square', 'ellipse', 'number', 'shade', 'color', 'pie'. Color intensity of the glyph is proportional to the correlation coefficients by default color setting.

corrplot.mixed() is a wrapped function for mixed visualization style, which can set the visual methods of lower and upper triangular separately.

There are three layout types (parameter type): 'full', 'upper' and 'lower'.

The correlation matrix can be reordered according to the correlation matrix coefficients. This is important to identify the hidden structure and pattern in the matrix.

library(corrplot)
## corrplot 0.92 loaded
M = cor(mtcars)
corrplot(M, method = 'number') # colorful number

corrplot(M, method = 'color', order = 'alphabet')

corrplot(M) # by default, method = 'circle'

corrplot(M, order = 'AOE') # after 'AOE' reorder

corrplot(M, method = 'shade', order = 'AOE', diag = FALSE)

corrplot(M, method = 'square', order = 'FPC', type = 'lower', diag = FALSE)

corrplot(M, method = 'ellipse', order = 'AOE', type = 'upper')

corrplot.mixed(M, order = 'AOE')

corrplot.mixed(M, lower = 'shade', upper = 'pie', order = 'hclust')

Reorder a correlation matrix

The details of four order algorithms, named 'AOE', 'FPC', 'hclust', 'alphabet' are as following.

You can also reorder the matrix ‘manually’ via function corrMatOrder().

If using 'hclust', corrplot() can draw rectangles around the plot of correlation matrix based on the results of hierarchical clustering.

corrplot(M, order = 'hclust', addrect = 2)

corrplot(M, method = 'square', diag = FALSE, order = 'hclust',
         addrect = 3, rect.col = 'blue', rect.lwd = 3, tl.pos = 'd')

R package seriation provides the infrastructure for ordering objects with an implementation of several seriation/sequencing/ordination techniques to reorder matrices, dissimilarity matrices, and dendrograms. For more information, see in section References.

We can reorder the matrix via seriation package and then corrplot it. Here are some examples.

library(seriation)
list_seriation_methods('matrix')
## [1] "BEA"       "BEA_TSP"   "Heatmap"   "Identity"  "PCA"       "PCA_angle"
## [7] "Random"    "Reverse"
list_seriation_methods('dist')
##  [1] "ARSA"          "BBURCG"        "BBWRCG"        "GW"           
##  [5] "GW_average"    "GW_complete"   "GW_single"     "GW_ward"      
##  [9] "HC"            "HC_average"    "HC_complete"   "HC_single"    
## [13] "HC_ward"       "Identity"      "MDS"           "MDS_angle"    
## [17] "MDS_metric"    "MDS_nonmetric" "OLO"           "OLO_average"  
## [21] "OLO_complete"  "OLO_single"    "OLO_ward"      "QAP_2SUM"     
## [25] "QAP_BAR"       "QAP_Inertia"   "QAP_LS"        "R2E"          
## [29] "Random"        "SA"            "SPIN_NH"       "SPIN_STS"     
## [33] "Spectral"      "Spectral_norm" "TSP"           "VAT"
data(Zoo)
Z = cor(Zoo[, -c(15, 17)])

dist2order = function(corr, method, ...) {
  d_corr = as.dist(1 - corr)
  s = seriate(d_corr, method = method, ...)
  i = get_order(s)
  return(i)
}

Methods 'PCA_angle' and 'HC' in seriation, are same as 'AOE' and 'hclust' separately in corrplot() and corrMatOrder().

Here are some plots after seriation.

# Fast Optimal Leaf Ordering for Hierarchical Clustering
i = dist2order(Z, 'OLO')
corrplot(Z[i, i], cl.pos = 'n')

# Quadratic Assignment Problem
i = dist2order(Z, 'QAP_2SUM')
corrplot(Z[i, i], cl.pos = 'n')

# Multidimensional Scaling
i = dist2order(Z, 'MDS_nonmetric')
corrplot(Z[i, i], cl.pos = 'n')

# Simulated annealing
i = dist2order(Z, 'ARSA')
corrplot(Z[i, i], cl.pos = 'n')

# TSP solver
i = dist2order(Z, 'TSP')
corrplot(Z[i, i], cl.pos = 'n')

# Spectral seriation
i = dist2order(Z, 'Spectral')
corrplot(Z[i, i], cl.pos = 'n')

corrRect() can add rectangles on the plot with three ways(parameter index, name and namesMat) after corrplot(). We can use pipe operator *>% in package magrittr with more convenience. Since R 4.1.0, |> is supported without extra package.

library(magrittr)

# Rank-two ellipse seriation, use index parameter
i = dist2order(Z, 'R2E')
corrplot(Z[i, i], cl.pos = 'n') %>% corrRect(c(1, 9, 15))

# use name parameter
# Since R 4.1.0, the following one line code works:
# corrplot(M, order = 'AOE') |> corrRect(name = c('gear', 'wt', 'carb'))
corrplot(Z, order = 'AOE') %>%
  corrRect(name = c('tail', 'airborne', 'venomous', 'predator'))

# use namesMat parameter
r = rbind(c('eggs', 'catsize', 'airborne', 'milk'),
          c('catsize', 'eggs', 'milk', 'airborne'))
corrplot(Z, order = 'hclust') %>% corrRect(namesMat = r)

Change color spectra, color-legend and text-legend

We can get sequential and diverging colors from COL1() and COL2(). The color palettes are borrowed from RColorBrewer package.

Notice: the middle color getting from COL2() is fixed to '#FFFFFF'(white), thus we can visualizing element 0 with white color.

The colors of the correlation plots can be customized by col in corrplot(). They are distributed uniformly in col.lim interval.

Here all diverging colors from COL2() and sequential colors from COL1() are shown below.

Diverging colors:

Sequential colors:

Usage of COL1() and COL2():

COL1(sequential = c("Oranges", "Purples", "Reds", "Blues", "Greens", 
                    "Greys", "OrRd", "YlOrRd", "YlOrBr", "YlGn"), n = 200)

COL2(diverging = c("RdBu", "BrBG", "PiYG", "PRGn", "PuOr", "RdYlBu"), n = 200)

In addition, function colorRampPalette() is very convenient for generating color spectrum.

Parameters group cl.* is for color-legend. The common-using are:

Parameters group tl.* is for text-legend. The common-using are:

corrplot(M, order = 'AOE', col = COL2('RdBu', 10))

corrplot(M, order = 'AOE', addCoef.col = 'black', tl.pos = 'd',
         cl.pos = 'n', col = COL2('PiYG'))

corrplot(M, method = 'square', order = 'AOE', addCoef.col = 'black', tl.pos = 'd',
         cl.pos = 'n', col = COL2('BrBG'))

## bottom color legend, diagonal text legend, rotate text label
corrplot(M, order = 'AOE', cl.pos = 'b', tl.pos = 'd',
         col = COL2('PRGn'), diag = FALSE)

## text labels rotated 45 degrees and  wider color legend with numbers right aligned
corrplot(M, type = 'lower', order = 'hclust', tl.col = 'black',
         cl.ratio = 0.2, tl.srt = 45, col = COL2('PuOr', 10))

## remove color legend, text legend and principal diagonal glyph
corrplot(M, order = 'AOE', cl.pos = 'n', tl.pos = 'n',
         col = c('white', 'black'), bg = 'gold2')

Visualize non-correlation matrix, NA value and math label

We can visualize a non-correlation matrix by set is.corr=FALSE, and assign colors by col.lim. If the matrix have both positive and negative values, the matrix transformation keep every values positiveness and negativeness.

If your matrix is rectangular, you can adjust the aspect ratio with the win.asp parameter to make the matrix rendered as a square.

## matrix in [20, 26], grid.col
N1 = matrix(runif(80, 20, 26), 8)
corrplot(N1, is.corr = FALSE, col.lim = c(20, 30), method = 'color', tl.pos = 'n',
         col = COL1('YlGn'), cl.pos = 'b', addgrid.col = 'white', addCoef.col = 'grey50')

## matrix in [-15, 10]
N2 = matrix(runif(80, -15, 10), 8)
corrplot(N2, is.corr = FALSE, method = 'color', col.lim = c(-15, 10), tl.pos = 'n',
         col = COL2('PiYG'), cl.pos = 'b', addCoef.col = 'grey50')

Notice: when is.corr is TRUE, col.lim only affect the color legend If you change it, the color on correlation matrix plot is still assigned on c(-1, 1)

# when is.corr=TRUE, col.lim only affect the color legend display
corrplot(M/2)

corrplot(M/2, col.lim=c(-0.5, 0.5))

By default, corrplot renders NA values as '?' characters. Using na.label parameter, it is possible to use a different value (max. two characters are supported).

Since version 0.78, it is possible to use plotmath expression in variable names. To activate plotmath rendering, prefix your label with '$'.

M2 = M
diag(M2) = NA
colnames(M2) = rep(c('$alpha+beta', '$alpha[0]', '$alpha[beta]'),
                   c(4, 4, 3))
rownames(M2) = rep(c('$Sigma[i]^n', '$sigma',  '$alpha[0]^100', '$alpha[beta]'),
                   c(2, 4, 2, 3))
corrplot(10*abs(M2), is.corr = FALSE, col.lim = c(0, 10), tl.cex = 1.5)

Visualize p-value and confidence interval

corrplot() can also visualize p-value and confidence interval on the correlation matrix plot. Here are some important parameters.

About p-value:

About confidence interval:

We can get p-value matrix and confidence intervals matrix by cor.mtest() which returns a list containing:

testRes = cor.mtest(mtcars, conf.level = 0.95)

## specialized the insignificant value according to the significant level
corrplot(M, p.mat = testRes$p, sig.level = 0.10, order = 'hclust', addrect = 2)

## leave blank on non-significant coefficient
## add significant correlation coefficients
corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig='blank',
         addCoef.col ='black', number.cex = 0.8, order = 'AOE', diag=FALSE)

## leave blank on non-significant coefficient
## add all correlation coefficients
corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig='blank',
         order = 'AOE', diag = FALSE)$corrPos -> p1
text(p1$x, p1$y, round(p1$corr, 2))

## add p-values on no significant coefficients
corrplot(M, p.mat = testRes$p, insig = 'p-value')

## add all p-values
corrplot(M, p.mat = testRes$p, insig = 'p-value', sig.level = -1)

## add significant level stars
corrplot(M, p.mat = testRes$p, method = 'color', diag = FALSE, type = 'upper',
         sig.level = c(0.001, 0.01, 0.05), pch.cex = 0.9,
         insig = 'label_sig', pch.col = 'grey20', order = 'AOE')

## add significant level stars and cluster rectangles
corrplot(M, p.mat = testRes$p, tl.pos = 'd', order = 'hclust', addrect = 2,
         insig = 'label_sig', sig.level = c(0.001, 0.01, 0.05),
         pch.cex = 0.9, pch.col = 'grey20')

Visualize confidence interval.

# Visualize confidence interval
corrplot(M, lowCI = testRes$lowCI, uppCI = testRes$uppCI, order = 'hclust',
         tl.pos = 'd', rect.col = 'navy', plotC = 'rect', cl.pos = 'n')

# Visualize confidence interval and cross the significant coefficients
corrplot(M, p.mat = testRes$p, lowCI = testRes$lowCI, uppCI = testRes$uppCI,
         addrect = 3, rect.col = 'navy', plotC = 'rect', cl.pos = 'n')

References