A visualization of a correlation matrix.
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"), add = FALSE, col = NULL, cl.lim = NULL, bg = "white", title = "", is.corr = TRUE, 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", 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, |
add |
Logical, if |
col |
Vector, the color of glyphs. It is distributed uniformly in
|
cl.lim |
The limits NOTICE: if you set |
bg |
The background color. |
title |
Character, title of the graph. |
is.corr |
Logical, whether the input matrix is a correlation matrix or
not. We can visualize the non-correlation matrix by setting
|
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 labels; If character,
it must be one of |
cl.length |
Integer, the number of number-text in colorlabel, passed to
|
cl.cex |
Numeric, cex of number-label in colorlabel, passed to
|
cl.ratio |
Numeric, to justify the width of colorlabel, 0.1~0.2 is suggested. |
cl.align.text |
Character, |
cl.offset |
Numeric, for number-label in colorlabel, 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. |
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 reordered correlation matrix.
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 (weitaiyun@gmail.com)
Viliam Simko (viliam.simko@gmail.com)
Michael Levy (michael.levy@healthcatalyst.com)
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 col0 <- colorRampPalette(c("white", "cyan", "#007FFF", "blue","#00007F")) col1 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "white", "cyan", "#007FFF", "blue","#00007F")) col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582", "#FDDBC7", "#FFFFFF", "#D1E5F0", "#92C5DE", "#4393C3", "#2166AC", "#053061")) col3 <- colorRampPalette(c("red", "white", "blue")) col4 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "#7FFF7F", "cyan", "#007FFF", "blue", "#00007F")) 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", col = col1(20), cl.length = 21, addCoef.col = "grey") corrplot(M, order = "AOE", col = col1(10), addCoef.col = "grey") corrplot(M, order = "AOE", col = col2(200)) corrplot(M, order = "AOE", col = col2(200), addCoef.col = "grey") corrplot(M, order = "AOE", col = col2(20), cl.length = 21, addCoef.col = "grey") corrplot(M, order = "AOE", col = col2(10), addCoef.col = "grey") corrplot(M, order = "AOE", col = col3(100)) corrplot(M, order = "AOE", col = col3(10)) corrplot(M, method = "color", col = col1(20), cl.length = 21, order = "AOE", addCoef.col = "grey") corrplot(M, method = "square", col = col2(200), order = "AOE") corrplot(M, method = "ellipse", col = col1(200), order = "AOE") corrplot(M, method = "shade", col = col3(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", cl.lim = c(0,1)) corrplot(abs(M), order = "AOE", col = col1(20), cl.lim = c(0,1)) # when is.corr=TRUE, cl.lim only affect the color legend # If you change it, the color is still assigned on [-1, 1] corrplot(M/2) corrplot(M/2, cl.lim=c(-0.5,0.5)) # when is.corr=FALSE, cl.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, cl.lim=c(-2, 2)) corrplot(M*2, is.corr = FALSE, cl.lim=c(-2, 2) * 2) corrplot(M*2, is.corr = FALSE, cl.lim=c(-2, 2) * 4) ## 0.5~0.6 corrplot(abs(M)/10+0.5, col=col0(10)) corrplot(abs(M)/10+0.5, is.corr = FALSE, cl.lim=c(0.5,0.6), col=col0(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, cl.lim = c(-100, 100)) ## visualize a matrix in [100, 300] ran2 <- ran + 200 # bad color corrplot(ran2, is.corr = FALSE, cl.lim = c(100, 300), col=col1(100)) # good color corrplot(ran2, is.corr = FALSE, cl.lim = c(100, 300), col=col0(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 = .2, cl.align = "l") corrplot(M, order = "AOE", cl.ratio = .2, cl.align = "c") corrplot(M, order = "AOE", cl.ratio = .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]) res1 <- cor.mtest(mtcars, conf.level = 0.95) res2 <- cor.mtest(mtcars, conf.level = 0.99) ## specialized the insignificant value according to the significant level corrplot(M, p.mat = res1$p, sig.level = 0.2) corrplot(M, p.mat = res1$p, sig.level = 0.05) corrplot(M, p.mat = res1$p, sig.level = 0.01) corrplot(M, p.mat = res1$p, insig = "blank") corrplot(M, p.mat = res1$p, insig = "p-value") corrplot(M, p.mat = res1$p, insig = "p-value", sig.level = -1) ## add all p-values corrplot(M, p.mat = res1$p, order = "hclust", insig = "blank", addrect = 3) corrplot(M, p.mat = res1$p, order = "hclust", insig = "pch", addrect = 3) ## plot confidence interval(0.95), "square" 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 interval(0.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
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.