Title: | Visualization of a Correlation Matrix |
---|---|
Description: | Provides a visual exploratory tool on correlation matrix that supports automatic variable reordering to help detect hidden patterns among variables. |
Authors: | Taiyun Wei [cre, aut], Viliam Simko [aut], Michael Levy [ctb], Yihui Xie [ctb], Yan Jin [ctb], Jeff Zemla [ctb], Moritz Freidank [ctb], Jun Cai [ctb], Tomas Protivinsky [ctb] |
Maintainer: | Taiyun Wei <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.94 |
Built: | 2024-09-01 06:18:22 UTC |
Source: | https://github.com/taiyun/corrplot |
The corrplot package is a graphical display of a correlation matrix, confidence interval or general matrix. It also contains some algorithms to do matrix reordering. In addition, corrplot is good at details, including choosing color, text labels, color labels, layout, etc.
Taiyun Wei ([email protected])
Viliam Simko ([email protected])
Maintainer: Taiyun Wei ([email protected])
Michael Friendly (2002). Corrgrams: Exploratory displays for correlation matrices. The American Statistician, 56, 316–324.
D.J. Murdoch, E.D. Chow (1996). A graphical display of large correlation matrices. The American Statistician, 50, 178–180.
The plotcorr
function in the ellipse
package and
corrgram
function in the corrgram
package has some
similarities.
Get sequential colors from palette theme name and n. The color palettes are from RColorBrewer. Sequential colors are suitable for visualize a non-negative or non-positive matrix (e.g. matrix in [0, 20], or [-100, -10], or [100, 500]).
COL1( sequential = c("Oranges", "Purples", "Reds", "Blues", "Greens", "Greys", "OrRd", "YlOrRd", "YlOrBr", "YlGn"), n = 200 )
COL1( sequential = c("Oranges", "Purples", "Reds", "Blues", "Greens", "Greys", "OrRd", "YlOrRd", "YlOrBr", "YlGn"), n = 200 )
sequential |
Sequential color Palettes |
n |
the number of colors (>= 1) to be in the palette. |
A character vector containing color names
Function colorRampPalette
, package RColorBrewer
## diverging colors par(mar = c(0, 0, 0, 0) + 0.1) plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n') col = c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu') for(i in 1:length(col)) { colorlegend(COL2(col[i]), -10:10/10, align = 'l', cex = 0.8, xlim = c(0, 1), ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE) text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2, cex = 0.8) } ## sequential colors par(mar = c(0, 0, 0, 0) + 0.1) plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n') col = c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd', 'YlOrRd', 'YlOrBr', 'YlGn') for(i in 1:length(col)) { colorlegend(COL1(col[i]), 0:10, align = 'l', cex = 0.8, xlim = c(0, 1), ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE) text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2) } ## other examples to show colorlegend function par(mar = rep(0, 4)) plot(0, xlim = c(0, 6), ylim = c(-0.5, 1.2), type = 'n') colorlegend(rainbow(100), 0:9) colorlegend(heat.colors(100), LETTERS[1:12], xlim = c(1, 2)) colorlegend(terrain.colors(100), 0:9, ratio.colbar = 0.6, lim.segment = c(0, 0.6), xlim = c(2, 3), align = 'l') colorlegend(topo.colors(100), 0:9, lim.segment = c(0, 0.6), xlim = c(3, 4), align = 'l', offset = 0) colorlegend(cm.colors(100), 1:5, xlim = c(4, 5)) colorlegend(sample(rainbow(12)), labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12), xlim = c(5, 6), align = 'r') colorlegend(colbar = grey(1:100 / 100), 1:10, col = 'red', align = 'l', xlim = c(0, 6), ylim = c(-0.5, -0.1), vertical = FALSE) colorlegend(sample(rainbow(12)), labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12), xlim = c(0, 6), ylim = c(1.1, 1.2), vertical = FALSE)
## diverging colors par(mar = c(0, 0, 0, 0) + 0.1) plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n') col = c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu') for(i in 1:length(col)) { colorlegend(COL2(col[i]), -10:10/10, align = 'l', cex = 0.8, xlim = c(0, 1), ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE) text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2, cex = 0.8) } ## sequential colors par(mar = c(0, 0, 0, 0) + 0.1) plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n') col = c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd', 'YlOrRd', 'YlOrBr', 'YlGn') for(i in 1:length(col)) { colorlegend(COL1(col[i]), 0:10, align = 'l', cex = 0.8, xlim = c(0, 1), ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE) text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2) } ## other examples to show colorlegend function par(mar = rep(0, 4)) plot(0, xlim = c(0, 6), ylim = c(-0.5, 1.2), type = 'n') colorlegend(rainbow(100), 0:9) colorlegend(heat.colors(100), LETTERS[1:12], xlim = c(1, 2)) colorlegend(terrain.colors(100), 0:9, ratio.colbar = 0.6, lim.segment = c(0, 0.6), xlim = c(2, 3), align = 'l') colorlegend(topo.colors(100), 0:9, lim.segment = c(0, 0.6), xlim = c(3, 4), align = 'l', offset = 0) colorlegend(cm.colors(100), 1:5, xlim = c(4, 5)) colorlegend(sample(rainbow(12)), labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12), xlim = c(5, 6), align = 'r') colorlegend(colbar = grey(1:100 / 100), 1:10, col = 'red', align = 'l', xlim = c(0, 6), ylim = c(-0.5, -0.1), vertical = FALSE) colorlegend(sample(rainbow(12)), labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12), xlim = c(0, 6), ylim = c(1.1, 1.2), vertical = FALSE)
Get diverging colors from palette theme name and n. The color palettes are from RColorBrewer, but with the middle color changing to '#FFFFFF'(white), thus we can visualize element 0 with white color. Diverging colors are suitable for visualize a matrix which elements are partly positive and partly negative (e.g. correlation matrix in [-1, 1], or [-20, 100]).
COL2(diverging = c("RdBu", "BrBG", "PiYG", "PRGn", "PuOr", "RdYlBu"), n = 200)
COL2(diverging = c("RdBu", "BrBG", "PiYG", "PRGn", "PuOr", "RdYlBu"), n = 200)
diverging |
Diverging color Palettes |
n |
the number of colors (>= 1) to be in the palette. |
A character vector containing color names
Function colorRampPalette
, package RColorBrewer
## diverging colors par(mar = c(0, 0, 0, 0) + 0.1) plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n') col = c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu') for(i in 1:length(col)) { colorlegend(COL2(col[i]), -10:10/10, align = 'l', cex = 0.8, xlim = c(0, 1), ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE) text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2, cex = 0.8) } ## sequential colors par(mar = c(0, 0, 0, 0) + 0.1) plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n') col = c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd', 'YlOrRd', 'YlOrBr', 'YlGn') for(i in 1:length(col)) { colorlegend(COL1(col[i]), 0:10, align = 'l', cex = 0.8, xlim = c(0, 1), ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE) text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2) } ## other examples to show colorlegend function par(mar = rep(0, 4)) plot(0, xlim = c(0, 6), ylim = c(-0.5, 1.2), type = 'n') colorlegend(rainbow(100), 0:9) colorlegend(heat.colors(100), LETTERS[1:12], xlim = c(1, 2)) colorlegend(terrain.colors(100), 0:9, ratio.colbar = 0.6, lim.segment = c(0, 0.6), xlim = c(2, 3), align = 'l') colorlegend(topo.colors(100), 0:9, lim.segment = c(0, 0.6), xlim = c(3, 4), align = 'l', offset = 0) colorlegend(cm.colors(100), 1:5, xlim = c(4, 5)) colorlegend(sample(rainbow(12)), labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12), xlim = c(5, 6), align = 'r') colorlegend(colbar = grey(1:100 / 100), 1:10, col = 'red', align = 'l', xlim = c(0, 6), ylim = c(-0.5, -0.1), vertical = FALSE) colorlegend(sample(rainbow(12)), labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12), xlim = c(0, 6), ylim = c(1.1, 1.2), vertical = FALSE)
## diverging colors par(mar = c(0, 0, 0, 0) + 0.1) plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n') col = c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu') for(i in 1:length(col)) { colorlegend(COL2(col[i]), -10:10/10, align = 'l', cex = 0.8, xlim = c(0, 1), ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE) text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2, cex = 0.8) } ## sequential colors par(mar = c(0, 0, 0, 0) + 0.1) plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n') col = c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd', 'YlOrRd', 'YlOrBr', 'YlGn') for(i in 1:length(col)) { colorlegend(COL1(col[i]), 0:10, align = 'l', cex = 0.8, xlim = c(0, 1), ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE) text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2) } ## other examples to show colorlegend function par(mar = rep(0, 4)) plot(0, xlim = c(0, 6), ylim = c(-0.5, 1.2), type = 'n') colorlegend(rainbow(100), 0:9) colorlegend(heat.colors(100), LETTERS[1:12], xlim = c(1, 2)) colorlegend(terrain.colors(100), 0:9, ratio.colbar = 0.6, lim.segment = c(0, 0.6), xlim = c(2, 3), align = 'l') colorlegend(topo.colors(100), 0:9, lim.segment = c(0, 0.6), xlim = c(3, 4), align = 'l', offset = 0) colorlegend(cm.colors(100), 1:5, xlim = c(4, 5)) colorlegend(sample(rainbow(12)), labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12), xlim = c(5, 6), align = 'r') colorlegend(colbar = grey(1:100 / 100), 1:10, col = 'red', align = 'l', xlim = c(0, 6), ylim = c(-0.5, -0.1), vertical = FALSE) colorlegend(sample(rainbow(12)), labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12), xlim = c(0, 6), ylim = c(1.1, 1.2), vertical = FALSE)
Draw color legend.
colorlegend( colbar, labels, at = NULL, xlim = c(0, 1), ylim = c(0, 1), vertical = TRUE, ratio.colbar = 0.4, lim.segment = "auto", align = c("c", "l", "r"), addlabels = TRUE, ... )
colorlegend( colbar, labels, at = NULL, xlim = c(0, 1), ylim = c(0, 1), vertical = TRUE, ratio.colbar = 0.4, lim.segment = "auto", align = c("c", "l", "r"), addlabels = TRUE, ... )
colbar |
Vector, color of colbar. |
labels |
Vector, numeric or character to be written. |
at |
Numeric vector (quantile), the position to put labels. See examples for details. |
xlim |
See in |
ylim |
See in |
vertical |
Logical, whether the colorlegend is vertical or horizon. |
ratio.colbar |
The width ratio of colorbar to the total colorlegend (including colorbar, segments and labels). |
lim.segment |
Vector (quantile) of length 2, the elements should be in [0,1], giving segments coordinates ranges. If the value is NULL or 'auto', then the ranges are derived automatically. |
align |
Character, alignment type of labels, |
addlabels |
Logical, whether add text label or not. |
... |
Additional arguments, passed to |
Taiyun Wei
## diverging colors par(mar = c(0, 0, 0, 0) + 0.1) plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n') col = c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu') for(i in 1:length(col)) { colorlegend(COL2(col[i]), -10:10/10, align = 'l', cex = 0.8, xlim = c(0, 1), ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE) text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2, cex = 0.8) } ## sequential colors par(mar = c(0, 0, 0, 0) + 0.1) plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n') col = c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd', 'YlOrRd', 'YlOrBr', 'YlGn') for(i in 1:length(col)) { colorlegend(COL1(col[i]), 0:10, align = 'l', cex = 0.8, xlim = c(0, 1), ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE) text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2) } ## other examples to show colorlegend function par(mar = rep(0, 4)) plot(0, xlim = c(0, 6), ylim = c(-0.5, 1.2), type = 'n') colorlegend(rainbow(100), 0:9) colorlegend(heat.colors(100), LETTERS[1:12], xlim = c(1, 2)) colorlegend(terrain.colors(100), 0:9, ratio.colbar = 0.6, lim.segment = c(0, 0.6), xlim = c(2, 3), align = 'l') colorlegend(topo.colors(100), 0:9, lim.segment = c(0, 0.6), xlim = c(3, 4), align = 'l', offset = 0) colorlegend(cm.colors(100), 1:5, xlim = c(4, 5)) colorlegend(sample(rainbow(12)), labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12), xlim = c(5, 6), align = 'r') colorlegend(colbar = grey(1:100 / 100), 1:10, col = 'red', align = 'l', xlim = c(0, 6), ylim = c(-0.5, -0.1), vertical = FALSE) colorlegend(sample(rainbow(12)), labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12), xlim = c(0, 6), ylim = c(1.1, 1.2), vertical = FALSE)
## diverging colors par(mar = c(0, 0, 0, 0) + 0.1) plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n') col = c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu') for(i in 1:length(col)) { colorlegend(COL2(col[i]), -10:10/10, align = 'l', cex = 0.8, xlim = c(0, 1), ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE) text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2, cex = 0.8) } ## sequential colors par(mar = c(0, 0, 0, 0) + 0.1) plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n') col = c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd', 'YlOrRd', 'YlOrBr', 'YlGn') for(i in 1:length(col)) { colorlegend(COL1(col[i]), 0:10, align = 'l', cex = 0.8, xlim = c(0, 1), ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE) text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2) } ## other examples to show colorlegend function par(mar = rep(0, 4)) plot(0, xlim = c(0, 6), ylim = c(-0.5, 1.2), type = 'n') colorlegend(rainbow(100), 0:9) colorlegend(heat.colors(100), LETTERS[1:12], xlim = c(1, 2)) colorlegend(terrain.colors(100), 0:9, ratio.colbar = 0.6, lim.segment = c(0, 0.6), xlim = c(2, 3), align = 'l') colorlegend(topo.colors(100), 0:9, lim.segment = c(0, 0.6), xlim = c(3, 4), align = 'l', offset = 0) colorlegend(cm.colors(100), 1:5, xlim = c(4, 5)) colorlegend(sample(rainbow(12)), labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12), xlim = c(5, 6), align = 'r') colorlegend(colbar = grey(1:100 / 100), 1:10, col = 'red', align = 'l', xlim = c(0, 6), ylim = c(-0.5, -0.1), vertical = FALSE) colorlegend(sample(rainbow(12)), labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12), xlim = c(0, 6), ylim = c(1.1, 1.2), vertical = FALSE)
Significance test which produces p-values and confidence intervals for each pair of input features.
cor.mtest(mat, ...)
cor.mtest(mat, ...)
mat |
Input matrix of size |
... |
Additional arguments passed to function |
Return a list containing:
p |
Square matrix of size |
lowCI |
Square matrix of size |
uppCI |
Square matrix of size |
Function cor.test
Draw rectangle(s) around the chart of corrrlation matrix based on the number of each cluster's members.
corrMatOrder( corr, order = c("AOE", "FPC", "hclust", "alphabet"), hclust.method = c("complete", "ward", "ward.D", "ward.D2", "single", "average", "mcquitty", "median", "centroid") )
corrMatOrder( corr, order = c("AOE", "FPC", "hclust", "alphabet"), hclust.method = c("complete", "ward", "ward.D", "ward.D2", "single", "average", "mcquitty", "median", "centroid") )
corr |
Correlation matrix to reorder. |
order |
Character, the ordering method for the correlation matrix.
|
hclust.method |
Character, the agglomeration method to be used when
|
Returns a single permutation vector.
Taiyun Wei
Package seriation
offers more methods to reorder matrices,
such as ARSA, BBURCG, BBWRCG, MDS, TSP, Chen and so forth.
M = cor(mtcars) (order.AOE = corrMatOrder(M, order = 'AOE')) (order.FPC = corrMatOrder(M, order = 'FPC')) (order.hc = corrMatOrder(M, order = 'hclust')) (order.hc2 = corrMatOrder(M, order = 'hclust', hclust.method = 'ward.D')) M.AOE = M[order.AOE, order.AOE] M.FPC = M[order.FPC, order.FPC] M.hc = M[order.hc, order.hc] M.hc2 = M[order.hc2, order.hc2] par(ask = TRUE) corrplot(M) corrplot(M.AOE) corrplot(M.FPC) corrplot(M.hc) corrplot(M.hc) corrRect.hclust(corr = M.hc, k = 2) corrplot(M.hc) corrRect.hclust(corr = M.hc, k = 3) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 2, method = 'ward.D')
M = cor(mtcars) (order.AOE = corrMatOrder(M, order = 'AOE')) (order.FPC = corrMatOrder(M, order = 'FPC')) (order.hc = corrMatOrder(M, order = 'hclust')) (order.hc2 = corrMatOrder(M, order = 'hclust', hclust.method = 'ward.D')) M.AOE = M[order.AOE, order.AOE] M.FPC = M[order.FPC, order.FPC] M.hc = M[order.hc, order.hc] M.hc2 = M[order.hc2, order.hc2] par(ask = TRUE) corrplot(M) corrplot(M.AOE) corrplot(M.FPC) corrplot(M.hc) corrplot(M.hc) corrRect.hclust(corr = M.hc, k = 2) corrplot(M.hc) corrRect.hclust(corr = M.hc, k = 3) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 2, method = 'ward.D')
A graphical display of a correlation matrix, confidence interval. The details
are paid great attention to. It can also visualize a general matrix by
setting is.corr = FALSE
.
corrplot( corr, method = c("circle", "square", "ellipse", "number", "shade", "color", "pie"), type = c("full", "lower", "upper"), col = NULL, col.lim = NULL, is.corr = TRUE, bg = "white", title = "", add = FALSE, diag = TRUE, outline = FALSE, mar = c(0, 0, 0, 0), addgrid.col = NULL, addCoef.col = NULL, addCoefasPercent = FALSE, order = c("original", "AOE", "FPC", "hclust", "alphabet"), hclust.method = c("complete", "ward", "ward.D", "ward.D2", "single", "average", "mcquitty", "median", "centroid"), addrect = NULL, rect.col = "black", rect.lwd = 2, tl.pos = NULL, tl.cex = 1, tl.col = "red", tl.offset = 0.4, tl.srt = 90, cl.pos = NULL, cl.length = NULL, cl.cex = 0.8, cl.ratio = 0.15, cl.align.text = "c", cl.offset = 0.5, number.cex = 1, number.font = 2, number.digits = NULL, addshade = c("negative", "positive", "all"), shade.lwd = 1, shade.col = "white", transKeepSign = TRUE, p.mat = NULL, sig.level = 0.05, insig = c("pch", "p-value", "blank", "n", "label_sig"), pch = 4, pch.col = "black", pch.cex = 3, plotCI = c("n", "square", "circle", "rect"), lowCI.mat = NULL, uppCI.mat = NULL, na.label = "?", na.label.col = "black", win.asp = 1, ... )
corrplot( corr, method = c("circle", "square", "ellipse", "number", "shade", "color", "pie"), type = c("full", "lower", "upper"), col = NULL, col.lim = NULL, is.corr = TRUE, bg = "white", title = "", add = FALSE, diag = TRUE, outline = FALSE, mar = c(0, 0, 0, 0), addgrid.col = NULL, addCoef.col = NULL, addCoefasPercent = FALSE, order = c("original", "AOE", "FPC", "hclust", "alphabet"), hclust.method = c("complete", "ward", "ward.D", "ward.D2", "single", "average", "mcquitty", "median", "centroid"), addrect = NULL, rect.col = "black", rect.lwd = 2, tl.pos = NULL, tl.cex = 1, tl.col = "red", tl.offset = 0.4, tl.srt = 90, cl.pos = NULL, cl.length = NULL, cl.cex = 0.8, cl.ratio = 0.15, cl.align.text = "c", cl.offset = 0.5, number.cex = 1, number.font = 2, number.digits = NULL, addshade = c("negative", "positive", "all"), shade.lwd = 1, shade.col = "white", transKeepSign = TRUE, p.mat = NULL, sig.level = 0.05, insig = c("pch", "p-value", "blank", "n", "label_sig"), pch = 4, pch.col = "black", pch.cex = 3, plotCI = c("n", "square", "circle", "rect"), lowCI.mat = NULL, uppCI.mat = NULL, na.label = "?", na.label.col = "black", win.asp = 1, ... )
corr |
The correlation matrix to visualize, must be square if
|
method |
Character, the visualization method of correlation matrix to be
used. Currently, it supports seven methods, named The areas of circles or squares show the absolute value of corresponding
correlation coefficients. Method |
type |
Character, |
col |
Vector, the colors of glyphs. They are distributed uniformly in
|
col.lim |
The limits NOTICE: if you set |
is.corr |
Logical, whether the input matrix is a correlation matrix or
not. We can visualize the non-correlation matrix by setting
|
bg |
The background color. |
title |
Character, title of the graph. |
add |
Logical, if |
diag |
Logical, whether display the correlation coefficients on the principal diagonal. |
outline |
Logical or character, whether plot outline of circles, square
and ellipse, or the color of these glyphs. For pie, this represents the
color of the circle outlining the pie. If |
mar |
See |
addgrid.col |
The color of the grid. If |
addCoef.col |
Color of coefficients added on the graph. If |
addCoefasPercent |
Logic, whether translate coefficients into percentage style for spacesaving. |
order |
Character, the ordering method of the correlation matrix.
See function |
hclust.method |
Character, the agglomeration method to be used when
|
addrect |
Integer, the number of rectangles draws on the graph according
to the hierarchical cluster, only valid when |
rect.col |
Color for rectangle border(s), only valid when |
rect.lwd |
Numeric, line width for borders for rectangle border(s), only
valid when |
tl.pos |
Character or logical, position of text labels. If character, it
must be one of |
tl.cex |
Numeric, for the size of text label (variable names). |
tl.col |
The color of text label. |
tl.offset |
Numeric, for text label, see |
tl.srt |
Numeric, for text label string rotation in degrees, see
|
cl.pos |
Character or logical, position of color-legend; If character,
it must be one of |
cl.length |
Integer, the number of number-text in color-legend, passed to
|
cl.cex |
Numeric, cex of number-label in color-legend, passed to
|
cl.ratio |
Numeric, to justify the width of color-legend, 0.1~0.2 is suggested. |
cl.align.text |
Character, |
cl.offset |
Numeric, for number-label in color-legend, see
|
number.cex |
The |
number.font |
the |
number.digits |
indicating the number of decimal digits to be added into the plot. Non-negative integer or NULL, default NULL. |
addshade |
Character for shade style, |
shade.lwd |
Numeric, the line width of shade. |
shade.col |
The color of shade line. |
transKeepSign |
Logical, whether or not to keep matrix values' sign when
transforming non-corr matrix for plotting.
Only valid when NOTE: If |
p.mat |
Matrix of p-value, if |
sig.level |
Significant level, if the p-value in |
insig |
Character, specialized insignificant correlation coefficients,
|
pch |
Add character on the glyphs of insignificant correlation
coefficients(only valid when |
pch.col |
The color of pch (only valid when |
pch.cex |
The cex of pch (only valid when |
plotCI |
Character, method of ploting confidence interval. If
|
lowCI.mat |
Matrix of the lower bound of confidence interval. |
uppCI.mat |
Matrix of the upper bound of confidence interval. |
na.label |
Label to be used for rendering |
na.label.col |
Color used for rendering |
win.asp |
Aspect ration for the whole plot. Value other than 1 is currently compatible only with methods 'circle' and 'square'. |
... |
Additional arguments passing to function |
corrplot
function offers flexible ways to visualize
correlation matrix, lower and upper bound of confidence interval matrix.
(Invisibly) returns a list(corr, corrTrans, arg)
.
corr
is a reordered correlation matrix for plotting.
corrPos
is a data frame with xName, yName, x, y, corr
and
p.value
(if p.mat is not NULL)
column, which x and y are the position on the correlation matrix plot.
arg
is a list of some corrplot() input parameters' value.
Now type
is in.
Cairo
and cairoDevice
packages is strongly recommended to
produce high-quality PNG, JPEG, TIFF bitmap files, especially for that
method
circle
, ellipse
.
Row- and column names of the input matrix are used as labels rendered
in the corrplot. Plothmath expressions will be used if the name is prefixed
by one of the following characters: :
, =
or $
.
For example ':alpha + beta'
.
Taiyun Wei ([email protected])
Viliam Simko ([email protected])
Michael Levy ([email protected])
Michael Friendly (2002). Corrgrams: Exploratory displays for correlation matrices. The American Statistician, 56, 316–324.
D.J. Murdoch, E.D. Chow (1996). A graphical display of large correlation matrices. The American Statistician, 50, 178–180.
Function plotcorr
in the ellipse
package and
corrgram
in the corrgram
package have some similarities.
Package seriation
offered more methods to reorder matrices, such as
ARSA, BBURCG, BBWRCG, MDS, TSP, Chen and so forth.
data(mtcars) M = cor(mtcars) set.seed(0) ## different color series ## COL2: Get diverging colors ## c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu') ## COL1: Get sequential colors ## c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd', 'YlOrRd', 'YlOrBr', 'YlGn') wb = c('white', 'black') par(ask = TRUE) ## different color scale and methods to display corr-matrix corrplot(M, method = 'number', col = 'black', cl.pos = 'n') corrplot(M, method = 'number') corrplot(M) corrplot(M, order = 'AOE') corrplot(M, order = 'AOE', addCoef.col = 'grey') corrplot(M, order = 'AOE', cl.length = 21, addCoef.col = 'grey') corrplot(M, order = 'AOE', col = COL2(n=10), addCoef.col = 'grey') corrplot(M, order = 'AOE', col = COL2('PiYG')) corrplot(M, order = 'AOE', col = COL2('PRGn'), addCoef.col = 'grey') corrplot(M, order = 'AOE', col = COL2('PuOr', 20), cl.length = 21, addCoef.col = 'grey') corrplot(M, order = 'AOE', col = COL2('PuOr', 10), addCoef.col = 'grey') corrplot(M, order = 'AOE', col = COL2('RdYlBu', 100)) corrplot(M, order = 'AOE', col = COL2('RdYlBu', 10)) corrplot(M, method = 'color', col = COL2(n=20), cl.length = 21, order = 'AOE', addCoef.col = 'grey') corrplot(M, method = 'square', col = COL2(n=200), order = 'AOE') corrplot(M, method = 'ellipse', col = COL2(n=200), order = 'AOE') corrplot(M, method = 'shade', col = COL2(n=20), order = 'AOE') corrplot(M, method = 'pie', order = 'AOE') ## col = wb corrplot(M, col = wb, order = 'AOE', outline = TRUE, cl.pos = 'n') ## like Chinese wiqi, suit for either on screen or white-black print. corrplot(M, col = wb, bg = 'gold2', order = 'AOE', cl.pos = 'n') ## mixed methods: It's more efficient if using function 'corrplot.mixed' ## circle + ellipse corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'd') corrplot(M, add = TRUE, type = 'lower', method = 'ellipse', order = 'AOE', diag = FALSE, tl.pos = 'n', cl.pos = 'n') ## circle + square corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'd') corrplot(M, add = TRUE, type = 'lower', method = 'square', order = 'AOE', diag = FALSE, tl.pos = 'n', cl.pos = 'n') ## circle + colorful number corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'd') corrplot(M, add = TRUE, type = 'lower', method = 'number', order = 'AOE', diag = FALSE, tl.pos = 'n', cl.pos = 'n') ## circle + black number corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'tp') corrplot(M, add = TRUE, type = 'lower', method = 'number', order = 'AOE', col = 'black', diag = FALSE, tl.pos = 'n', cl.pos = 'n') ## order is hclust and draw rectangles corrplot(M, order = 'hclust') corrplot(M, order = 'hclust', addrect = 2) corrplot(M, order = 'hclust', addrect = 3, rect.col = 'red') corrplot(M, order = 'hclust', addrect = 4, rect.col = 'blue') corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 4) ## visualize a matrix in [0, 1] corrplot(abs(M), order = 'AOE', col.lim = c(0, 1)) corrplot(abs(M), order = 'AOE', is.corr = FALSE, col.lim = c(0, 1)) # when is.corr=TRUE, col.lim only affect the color legend # If you change it, the color is still assigned on [-1, 1] corrplot(M/2) corrplot(M/2, col.lim = c(-0.5, 0.5)) # when is.corr=FALSE, col.lim is also used to assign colors # if the matrix have both positive and negative values # the matrix transformation keep every values positive and negative corrplot(M*2, is.corr = FALSE, col.lim = c(-2, 2)) corrplot(M*2, is.corr = FALSE, col.lim = c(-2, 2) * 2) corrplot(M*2, is.corr = FALSE, col.lim = c(-2, 2) * 4) ## 0.5~0.6 corrplot(abs(M)/10+0.5, col = COL1('Greens', 10)) corrplot(abs(M)/10+0.5, is.corr = FALSE, col.lim = c(0.5, 0.6), col = COL1('YlGn', 10)) ## visualize a matrix in [-100, 100] ran = round(matrix(runif(225, -100, 100), 15)) corrplot(ran, is.corr = FALSE) corrplot(ran, is.corr = FALSE, col.lim = c(-100, 100)) ## visualize a matrix in [100, 300] ran2 = ran + 200 # bad color, not suitable for a matrix in [100, 300] corrplot(ran2, is.corr = FALSE, col.lim = c(100, 300), col = COL2(, 100)) # good color corrplot(ran2, is.corr = FALSE, col.lim = c(100, 300), col = COL1(, 100)) ## text-labels and plot type corrplot(M, order = 'AOE', tl.srt = 45) corrplot(M, order = 'AOE', tl.srt = 60) corrplot(M, order = 'AOE', tl.pos = 'd', cl.pos = 'n') corrplot(M, order = 'AOE', diag = FALSE, tl.pos = 'd') corrplot(M, order = 'AOE', type = 'upper') corrplot(M, order = 'AOE', type = 'upper', diag = FALSE) corrplot(M, order = 'AOE', type = 'lower', cl.pos = 'b') corrplot(M, order = 'AOE', type = 'lower', cl.pos = 'b', diag = FALSE) #### color-legend corrplot(M, order = 'AOE', cl.ratio = 0.2, cl.align = 'l') corrplot(M, order = 'AOE', cl.ratio = 0.2, cl.align = 'c') corrplot(M, order = 'AOE', cl.ratio = 0.2, cl.align = 'r') corrplot(M, order = 'AOE', cl.pos = 'b') corrplot(M, order = 'AOE', cl.pos = 'b', tl.pos = 'd') corrplot(M, order = 'AOE', cl.pos = 'n') ## deal with missing Values M2 = M diag(M2) = NA corrplot(M2) corrplot(M2, na.label = 'o') corrplot(M2, na.label = 'NA') ##the input matrix is not square corrplot(M[1:8, ]) corrplot(M[, 1:8]) 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.05, order = 'hclust', addrect = 2) ## leave blank on no significant coefficient corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig ='blank', addCoef.col ='black', number.cex = 0.8, order = 'AOE', diag = FALSE) ## 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 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') res1 = cor.mtest(mtcars, conf.level = 0.95) res2 = cor.mtest(mtcars, conf.level = 0.99) ## plot confidence interval(0.95), 'circle' method corrplot(M, low = res1$uppCI, upp = res1$uppCI, plotCI = 'circle', addg = 'grey20', cl.pos = 'n') corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI, plotCI = 'circle', addg = 'grey20', cl.pos = 'n') corrplot(M, low = res1$lowCI, upp = res1$uppCI, col = c('white', 'black'), bg = 'gold2', order = 'AOE', plotCI = 'circle', cl.pos = 'n', pch.col = 'red') corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI, col = c('white', 'black'), bg = 'gold2', order = 'AOE', plotCI = 'circle', cl.pos = 'n', pch.col = 'red') ## plot confidence interval(0.95), 'square' method corrplot(M, low = res1$lowCI, upp = res1$uppCI, col = c('white', 'black'), bg = 'gold2', order = 'AOE', plotCI = 'square', addg = NULL, cl.pos = 'n') corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI, col = c('white', 'black'), bg = 'gold2', order = 'AOE', pch.col = 'red', plotCI = 'square', addg = NULL, cl.pos = 'n') ## plot confidence interval0.95, 0.95, 0.99, 'rect' method corrplot(M, low = res1$lowCI, upp = res1$uppCI, order = 'hclust', rect.col = 'navy', plotCI = 'rect', cl.pos = 'n') corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI, order = 'hclust', pch.col = 'red', sig.level = 0.05, addrect = 3, rect.col = 'navy', plotCI = 'rect', cl.pos = 'n') corrplot(M, p.mat = res2$p, low = res2$lowCI, upp = res2$uppCI, order = 'hclust', pch.col = 'red', sig.level = 0.01, addrect = 3, rect.col = 'navy', plotCI = 'rect', cl.pos = 'n') ## an animation of changing confidence interval in different significance level ## begin.animaton par(ask = FALSE) for (i in seq(0.1, 0, -0.005)) { tmp = cor.mtest(mtcars, conf.level = 1 - i) corrplot(M, p.mat = tmp$p, low = tmp$lowCI, upp = tmp$uppCI, order = 'hclust', pch.col = 'red', sig.level = i, plotCI = 'rect', cl.pos = 'n', mar = c(0, 0, 1, 0), title = substitute(alpha == x, list(x = format(i, digits = 3, nsmall = 3)))) Sys.sleep(0.15) } ## end.animaton
data(mtcars) M = cor(mtcars) set.seed(0) ## different color series ## COL2: Get diverging colors ## c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu') ## COL1: Get sequential colors ## c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd', 'YlOrRd', 'YlOrBr', 'YlGn') wb = c('white', 'black') par(ask = TRUE) ## different color scale and methods to display corr-matrix corrplot(M, method = 'number', col = 'black', cl.pos = 'n') corrplot(M, method = 'number') corrplot(M) corrplot(M, order = 'AOE') corrplot(M, order = 'AOE', addCoef.col = 'grey') corrplot(M, order = 'AOE', cl.length = 21, addCoef.col = 'grey') corrplot(M, order = 'AOE', col = COL2(n=10), addCoef.col = 'grey') corrplot(M, order = 'AOE', col = COL2('PiYG')) corrplot(M, order = 'AOE', col = COL2('PRGn'), addCoef.col = 'grey') corrplot(M, order = 'AOE', col = COL2('PuOr', 20), cl.length = 21, addCoef.col = 'grey') corrplot(M, order = 'AOE', col = COL2('PuOr', 10), addCoef.col = 'grey') corrplot(M, order = 'AOE', col = COL2('RdYlBu', 100)) corrplot(M, order = 'AOE', col = COL2('RdYlBu', 10)) corrplot(M, method = 'color', col = COL2(n=20), cl.length = 21, order = 'AOE', addCoef.col = 'grey') corrplot(M, method = 'square', col = COL2(n=200), order = 'AOE') corrplot(M, method = 'ellipse', col = COL2(n=200), order = 'AOE') corrplot(M, method = 'shade', col = COL2(n=20), order = 'AOE') corrplot(M, method = 'pie', order = 'AOE') ## col = wb corrplot(M, col = wb, order = 'AOE', outline = TRUE, cl.pos = 'n') ## like Chinese wiqi, suit for either on screen or white-black print. corrplot(M, col = wb, bg = 'gold2', order = 'AOE', cl.pos = 'n') ## mixed methods: It's more efficient if using function 'corrplot.mixed' ## circle + ellipse corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'd') corrplot(M, add = TRUE, type = 'lower', method = 'ellipse', order = 'AOE', diag = FALSE, tl.pos = 'n', cl.pos = 'n') ## circle + square corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'd') corrplot(M, add = TRUE, type = 'lower', method = 'square', order = 'AOE', diag = FALSE, tl.pos = 'n', cl.pos = 'n') ## circle + colorful number corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'd') corrplot(M, add = TRUE, type = 'lower', method = 'number', order = 'AOE', diag = FALSE, tl.pos = 'n', cl.pos = 'n') ## circle + black number corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'tp') corrplot(M, add = TRUE, type = 'lower', method = 'number', order = 'AOE', col = 'black', diag = FALSE, tl.pos = 'n', cl.pos = 'n') ## order is hclust and draw rectangles corrplot(M, order = 'hclust') corrplot(M, order = 'hclust', addrect = 2) corrplot(M, order = 'hclust', addrect = 3, rect.col = 'red') corrplot(M, order = 'hclust', addrect = 4, rect.col = 'blue') corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 4) ## visualize a matrix in [0, 1] corrplot(abs(M), order = 'AOE', col.lim = c(0, 1)) corrplot(abs(M), order = 'AOE', is.corr = FALSE, col.lim = c(0, 1)) # when is.corr=TRUE, col.lim only affect the color legend # If you change it, the color is still assigned on [-1, 1] corrplot(M/2) corrplot(M/2, col.lim = c(-0.5, 0.5)) # when is.corr=FALSE, col.lim is also used to assign colors # if the matrix have both positive and negative values # the matrix transformation keep every values positive and negative corrplot(M*2, is.corr = FALSE, col.lim = c(-2, 2)) corrplot(M*2, is.corr = FALSE, col.lim = c(-2, 2) * 2) corrplot(M*2, is.corr = FALSE, col.lim = c(-2, 2) * 4) ## 0.5~0.6 corrplot(abs(M)/10+0.5, col = COL1('Greens', 10)) corrplot(abs(M)/10+0.5, is.corr = FALSE, col.lim = c(0.5, 0.6), col = COL1('YlGn', 10)) ## visualize a matrix in [-100, 100] ran = round(matrix(runif(225, -100, 100), 15)) corrplot(ran, is.corr = FALSE) corrplot(ran, is.corr = FALSE, col.lim = c(-100, 100)) ## visualize a matrix in [100, 300] ran2 = ran + 200 # bad color, not suitable for a matrix in [100, 300] corrplot(ran2, is.corr = FALSE, col.lim = c(100, 300), col = COL2(, 100)) # good color corrplot(ran2, is.corr = FALSE, col.lim = c(100, 300), col = COL1(, 100)) ## text-labels and plot type corrplot(M, order = 'AOE', tl.srt = 45) corrplot(M, order = 'AOE', tl.srt = 60) corrplot(M, order = 'AOE', tl.pos = 'd', cl.pos = 'n') corrplot(M, order = 'AOE', diag = FALSE, tl.pos = 'd') corrplot(M, order = 'AOE', type = 'upper') corrplot(M, order = 'AOE', type = 'upper', diag = FALSE) corrplot(M, order = 'AOE', type = 'lower', cl.pos = 'b') corrplot(M, order = 'AOE', type = 'lower', cl.pos = 'b', diag = FALSE) #### color-legend corrplot(M, order = 'AOE', cl.ratio = 0.2, cl.align = 'l') corrplot(M, order = 'AOE', cl.ratio = 0.2, cl.align = 'c') corrplot(M, order = 'AOE', cl.ratio = 0.2, cl.align = 'r') corrplot(M, order = 'AOE', cl.pos = 'b') corrplot(M, order = 'AOE', cl.pos = 'b', tl.pos = 'd') corrplot(M, order = 'AOE', cl.pos = 'n') ## deal with missing Values M2 = M diag(M2) = NA corrplot(M2) corrplot(M2, na.label = 'o') corrplot(M2, na.label = 'NA') ##the input matrix is not square corrplot(M[1:8, ]) corrplot(M[, 1:8]) 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.05, order = 'hclust', addrect = 2) ## leave blank on no significant coefficient corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig ='blank', addCoef.col ='black', number.cex = 0.8, order = 'AOE', diag = FALSE) ## 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 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') res1 = cor.mtest(mtcars, conf.level = 0.95) res2 = cor.mtest(mtcars, conf.level = 0.99) ## plot confidence interval(0.95), 'circle' method corrplot(M, low = res1$uppCI, upp = res1$uppCI, plotCI = 'circle', addg = 'grey20', cl.pos = 'n') corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI, plotCI = 'circle', addg = 'grey20', cl.pos = 'n') corrplot(M, low = res1$lowCI, upp = res1$uppCI, col = c('white', 'black'), bg = 'gold2', order = 'AOE', plotCI = 'circle', cl.pos = 'n', pch.col = 'red') corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI, col = c('white', 'black'), bg = 'gold2', order = 'AOE', plotCI = 'circle', cl.pos = 'n', pch.col = 'red') ## plot confidence interval(0.95), 'square' method corrplot(M, low = res1$lowCI, upp = res1$uppCI, col = c('white', 'black'), bg = 'gold2', order = 'AOE', plotCI = 'square', addg = NULL, cl.pos = 'n') corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI, col = c('white', 'black'), bg = 'gold2', order = 'AOE', pch.col = 'red', plotCI = 'square', addg = NULL, cl.pos = 'n') ## plot confidence interval0.95, 0.95, 0.99, 'rect' method corrplot(M, low = res1$lowCI, upp = res1$uppCI, order = 'hclust', rect.col = 'navy', plotCI = 'rect', cl.pos = 'n') corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI, order = 'hclust', pch.col = 'red', sig.level = 0.05, addrect = 3, rect.col = 'navy', plotCI = 'rect', cl.pos = 'n') corrplot(M, p.mat = res2$p, low = res2$lowCI, upp = res2$uppCI, order = 'hclust', pch.col = 'red', sig.level = 0.01, addrect = 3, rect.col = 'navy', plotCI = 'rect', cl.pos = 'n') ## an animation of changing confidence interval in different significance level ## begin.animaton par(ask = FALSE) for (i in seq(0.1, 0, -0.005)) { tmp = cor.mtest(mtcars, conf.level = 1 - i) corrplot(M, p.mat = tmp$p, low = tmp$lowCI, upp = tmp$uppCI, order = 'hclust', pch.col = 'red', sig.level = i, plotCI = 'rect', cl.pos = 'n', mar = c(0, 0, 1, 0), title = substitute(alpha == x, list(x = format(i, digits = 3, nsmall = 3)))) Sys.sleep(0.15) } ## end.animaton
Using mixed methods to visualize a correlation matrix.
corrplot.mixed( corr, lower = "number", upper = "circle", tl.pos = c("d", "lt", "n"), diag = c("n", "l", "u"), bg = "white", addgrid.col = "grey", lower.col = NULL, upper.col = NULL, plotCI = c("n", "square", "circle", "rect"), mar = c(0, 0, 0, 0), ... )
corrplot.mixed( corr, lower = "number", upper = "circle", tl.pos = c("d", "lt", "n"), diag = c("n", "l", "u"), bg = "white", addgrid.col = "grey", lower.col = NULL, upper.col = NULL, plotCI = c("n", "square", "circle", "rect"), mar = c(0, 0, 0, 0), ... )
corr |
Matrix, the correlation matrix to visualize. |
lower |
Character, the visualization method for the lower triangular correlation matrix. |
upper |
Character, the visualization method for the upper triangular correlation matrix. |
tl.pos |
Character, |
diag |
Character, for specifying the glyph on the principal diagonal. It
is one of |
bg |
The background color. |
addgrid.col |
See the |
lower.col |
Passed as |
upper.col |
Passed as |
plotCI |
See the |
mar |
See |
... |
Additional arguments for corrplot's wrappers |
Taiyun Wei
M = cor(mtcars) ord = corrMatOrder(M, order = 'AOE') M2 = M[ord, ord] corrplot.mixed(M2) corrplot.mixed(M2, lower = 'ellipse', upper = 'circle') corrplot.mixed(M2, lower = 'square', upper = 'circle') corrplot.mixed(M2, lower = 'shade', upper = 'circle') corrplot.mixed(M2, tl.pos = 'lt') corrplot.mixed(M2, tl.pos = 'lt', diag = 'u') corrplot.mixed(M2, tl.pos = 'lt', diag = 'l') corrplot.mixed(M2, tl.pos = 'n')
M = cor(mtcars) ord = corrMatOrder(M, order = 'AOE') M2 = M[ord, ord] corrplot.mixed(M2) corrplot.mixed(M2, lower = 'ellipse', upper = 'circle') corrplot.mixed(M2, lower = 'square', upper = 'circle') corrplot.mixed(M2, lower = 'shade', upper = 'circle') corrplot.mixed(M2, tl.pos = 'lt') corrplot.mixed(M2, tl.pos = 'lt', diag = 'u') corrplot.mixed(M2, tl.pos = 'lt', diag = 'l') corrplot.mixed(M2, tl.pos = 'n')
Draw rectangle(s) after the correlation matrix plotted. SUGGESTION: It's more convenient to draw rectangle(s) by using pipe operator '|>' since R 4.1.0.
corrRect( corrRes = NULL, index = NULL, name = NULL, namesMat = NULL, col = "black", lwd = 2, ... )
corrRect( corrRes = NULL, index = NULL, name = NULL, namesMat = NULL, col = "black", lwd = 2, ... )
corrRes |
List of the |
index |
Vector, variable index of diag rect |
name |
Vector, variable name of diag rect |
namesMat |
4-length character vector or 4-columns character matrix,
represents the names of xleft, ybottom, xright, ytop correspondingly.
It needs |
col |
Color of rectangles. |
lwd |
Line width of rectangles. |
... |
Additional arguments passing to function |
corrRect
needs one of index
, name
and namesMat
inputted.
While corrRect.hclust
can get the members in each cluster
based on hierarchical clustering (hclust
).
(Invisibly) returns input parameter corrRes
,
usually list(corr, corrTrans, arg)
.
Taiyun Wei
data(mtcars) M = cor(mtcars) r = rbind(c('gear', 'wt', 'qsec', 'carb'), c('wt', 'gear', 'carb', 'qsec')) corrplot(M, order = 'AOE') -> p corrRect(p, namesMat = r) # same as using pipe operator `|>` if R version >= 4.1.0: # corrplot(M, order = 'AOE') |> corrRect(namesMat = r) r = c('gear', 'carb', 'qsec', 'wt') corrplot(M, order = 'AOE', type='lower') -> p corrRect(p, namesMat = r) # same as using pipe operator `|>` if R version >= 4.1.0: # corrplot(M, order = 'AOE', type='lower') |> corrRect(namesMat = r) corrplot(M, order = 'hclust', type = 'upper') -> p corrRect(p, index = c(1, 6, 11)) # same as using pipe operator `|>` if R version >= 4.1.0: # corrplot(M, order = 'AOE', type='lower') |> corrRect(index = c(1, 6, 11)) corrplot(M, order = 'hclust') -> p corrRect(p, name = c('carb', 'qsec', 'gear')) # same as using pipe operator `|>` if R version >= 4.1.0: # corrplot(M, order = 'hclust') |> corrRect(name = c('carb', 'qsec', 'gear')) (order.hc = corrMatOrder(M, order = 'hclust')) (order.hc2 = corrMatOrder(M, order = 'hclust', hclust.method = 'ward.D')) M.hc = M[order.hc, order.hc] M.hc2 = M[order.hc2, order.hc2] par(ask = TRUE) # same as: corrplot(M, order = 'hclust', addrect = 2) corrplot(M.hc) corrRect.hclust(corr = M.hc, k = 2) # same as: corrplot(M, order = 'hclust', addrect = 3) corrplot(M.hc) corrRect.hclust(corr = M.hc, k = 3) # same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D', addrect = 2) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 2, method = 'ward.D') # same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D', addrect = 3) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 3, method = 'ward.D') # same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D', addrect = 4) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 4, method = 'ward.D')
data(mtcars) M = cor(mtcars) r = rbind(c('gear', 'wt', 'qsec', 'carb'), c('wt', 'gear', 'carb', 'qsec')) corrplot(M, order = 'AOE') -> p corrRect(p, namesMat = r) # same as using pipe operator `|>` if R version >= 4.1.0: # corrplot(M, order = 'AOE') |> corrRect(namesMat = r) r = c('gear', 'carb', 'qsec', 'wt') corrplot(M, order = 'AOE', type='lower') -> p corrRect(p, namesMat = r) # same as using pipe operator `|>` if R version >= 4.1.0: # corrplot(M, order = 'AOE', type='lower') |> corrRect(namesMat = r) corrplot(M, order = 'hclust', type = 'upper') -> p corrRect(p, index = c(1, 6, 11)) # same as using pipe operator `|>` if R version >= 4.1.0: # corrplot(M, order = 'AOE', type='lower') |> corrRect(index = c(1, 6, 11)) corrplot(M, order = 'hclust') -> p corrRect(p, name = c('carb', 'qsec', 'gear')) # same as using pipe operator `|>` if R version >= 4.1.0: # corrplot(M, order = 'hclust') |> corrRect(name = c('carb', 'qsec', 'gear')) (order.hc = corrMatOrder(M, order = 'hclust')) (order.hc2 = corrMatOrder(M, order = 'hclust', hclust.method = 'ward.D')) M.hc = M[order.hc, order.hc] M.hc2 = M[order.hc2, order.hc2] par(ask = TRUE) # same as: corrplot(M, order = 'hclust', addrect = 2) corrplot(M.hc) corrRect.hclust(corr = M.hc, k = 2) # same as: corrplot(M, order = 'hclust', addrect = 3) corrplot(M.hc) corrRect.hclust(corr = M.hc, k = 3) # same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D', addrect = 2) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 2, method = 'ward.D') # same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D', addrect = 3) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 3, method = 'ward.D') # same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D', addrect = 4) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 4, method = 'ward.D')
Draw rectangles on the correlation matrix graph based on hierarchical cluster
(hclust
).
corrRect.hclust( corr, k = 2, col = "black", lwd = 2, method = c("complete", "ward", "ward.D", "ward.D2", "single", "average", "mcquitty", "median", "centroid") )
corrRect.hclust( corr, k = 2, col = "black", lwd = 2, method = c("complete", "ward", "ward.D", "ward.D2", "single", "average", "mcquitty", "median", "centroid") )
corr |
Correlation matrix for function |
k |
Integer, the number of rectangles drawn on the graph according to
the hierarchical cluster, for function |
col |
Color of rectangles. |
lwd |
Line width of rectangles. |
method |
Character, the agglomeration method to be used for hierarchical
clustering ( |
Taiyun Wei
data(mtcars) M = cor(mtcars) corrplot(M, order = 'FPC') -> p corrRect(p, index = c(1, 6, 11)) if(getRversion() >= '4.1.0') { corrplot(M, order = 'FPC') |> corrRect(index = c(1, 6, 11)) } (order.hc = corrMatOrder(M, order = 'hclust')) (order.hc2 = corrMatOrder(M, order = 'hclust', hclust.method = 'ward.D2')) M.hc = M[order.hc, order.hc] M.hc2 = M[order.hc2, order.hc2] par(ask = TRUE) # same as: corrplot(M, order = 'hclust', addrect = 2) corrplot(M.hc) corrRect.hclust(corr = M.hc, k = 2) # same as: corrplot(M, order = 'hclust', addrect = 3) corrplot(M.hc) corrRect.hclust(corr = M.hc, k = 3) # same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 2) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 2, method = 'ward.D2') # same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 3) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 3, method = 'ward.D2') # same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 4) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 4, method = 'ward.D2')
data(mtcars) M = cor(mtcars) corrplot(M, order = 'FPC') -> p corrRect(p, index = c(1, 6, 11)) if(getRversion() >= '4.1.0') { corrplot(M, order = 'FPC') |> corrRect(index = c(1, 6, 11)) } (order.hc = corrMatOrder(M, order = 'hclust')) (order.hc2 = corrMatOrder(M, order = 'hclust', hclust.method = 'ward.D2')) M.hc = M[order.hc, order.hc] M.hc2 = M[order.hc2, order.hc2] par(ask = TRUE) # same as: corrplot(M, order = 'hclust', addrect = 2) corrplot(M.hc) corrRect.hclust(corr = M.hc, k = 2) # same as: corrplot(M, order = 'hclust', addrect = 3) corrplot(M.hc) corrRect.hclust(corr = M.hc, k = 3) # same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 2) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 2, method = 'ward.D2') # same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 3) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 3, method = 'ward.D2') # same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 4) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 4, method = 'ward.D2')