R语言中的函数11:corrplot::corrplot()和corrplot.mixed()

corrplot()

介绍

该函数实现了对correlation矩阵和置信区间的图示化。并且它也可以对一般的矩阵实现可视化只需要设置is.corr = FALSE.

用法

corrplot(
  corr,
  method = c("circle", "square", "ellipse", "number", "shade", "color", "pie"),
  type = c("full", "lower", "upper"),
  add = FALSE,
  col = NULL,
  col.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
    用于可视化的相关系数矩阵,必须是方阵。对于一般的矩阵需要使用is.corr = FALSE.

  • method :
    字符。用于实现相关系数矩阵的方法。目前,支持的方法有 ‘circle’ (default), ‘square’, ‘ellipse’, ‘number’, ‘pie’, ‘shade’ and ‘color’. 其中,圆和正方形的面积表示相关系数绝对值的大小。

  • type
    字符。 ‘full’ (default), ‘upper’ 或者’lower’, 将展示出全矩阵,下三角矩阵或者上三角矩阵。

  • add
    逻辑值。如果是 TRUE, 该图形将添加到已存在的作图系统里。

  • col:
    向量。表示字符的颜色。如果为NULL,col将会是colorRampPalette(col2)(200).

  • col.lim
    限制进行分配颜色的区间 (x1, x2)。当is.corr是TRUE时,col.lim=ULL表示 col.lim=c(-1, 1) ; 当当is.corr是FALSE时, col.lim=NUL表示col.lim=c(min(corr), max(corr)) .
    注意:当is.corr=TRUE时,尽管我们对col.lim进行赋值,色标的分配依然是在[-1, 1]中, 它只会影响色标标注的显示.

  • bg
    背景色

  • title
    字符。图形的标题。

  • is.corr

  • 逻辑值。 当输入的是相关系数矩阵赋值为TRUE,一般矩阵赋值为FALSE.

  • diag :
    逻辑值。是否显示对角线上的相关系数。

  • outline :
    逻辑值或者字符。表示是否作出circles, square 和ellipse的轮廓线, 或者这些字符的颜色。对于pie, 这表示pie的颜色轮廓。如果outline=TRUE, 表示默认值为black.

  • mar :和参数par类似。

  • addgrid.col
    网格的颜色。如果是NA, 则不添加grid. 如果是NULL, 表示选择了默认值。而默认值是依赖于method的,如果method是 color或者shade, 网格的颜色是NA,也即不作出网格线。

  • addCoef.col
    将图形中的相关系数数字添加颜色。如果是NULL (default), 表示不显示数字。

  • addCoefasPercent :
    逻辑值。表示是否将系数转化为百分比的形式。

  • order
    字符。对相关系数矩阵的排序方式。
    取值有:

    • ‘original’ for original order (default).
    • ‘AOE’ for the angular order of the eigenvectors.
    • ‘FPC’ for the first principal component order.
    • ‘hclust’ for the hierarchical clustering order.
    • ‘alphabet’ for alphabetical order.
      See function corrMatOrder for details.
  • hclust.method:
    字符, 如果order=“hclust”,就表示聚类的方法,可选的有 ‘ward’, ‘ward.D’, ‘ward.D2’, ‘single’, ‘complete’, ‘average’, ‘mcquitty’, ‘median’ or ‘centroid’.

  • addrect:
    整数。作出的举行个数,基于的是聚类的等级。只有当order="hclust"时,才有作用。默认值是NULL表示不添加矩形。

  • rect.col
    矩形边框的颜色,只有当addrect是合适的或者大于1的时候才合法。

  • rect.lwd :
    数值。矩形的轮廓线宽,只有当addrect是合适的或者大于1的时候才合法。

  • tl.pos:
    字符或者逻辑值。表示labels的位置。如果是字符则必须是 ‘lt’, ‘ld’, ‘td’, ‘d’ or ‘n’. ‘lt’(default if type==‘full’) means left and top, ‘ld’(default if type==‘lower’) means left and diagonal, ‘td’(default if type==‘upper’) means top and diagonal(near), ‘d’ means diagonal, ‘n’ means don’t add text-label.

  • tl.cex :
    数值,表示文本标注(变量名)的字符大小.

  • tl.col:
    文本标注的颜色。

  • tl.offset :
    数值。用于对文本标注的处理。可见text.

  • tl.srt
    数值。表示文本标注字符串旋转的角度,可见text.

  • cl.pos:
    字符或者逻辑值。表示色标的位置。如果是字符,必须是其中之一’r’ (default if type==‘upper’ or ‘full’), ‘b’ (default if type==‘lower’) or ‘n’, ‘n’ means don’t draw color-legend.

  • cl.length:
    整数。the number of number-text in color-legend, passed to colorlegend. If NULL, cl.length is length(col) + 1 when length(col) <=20; cl.length is 11 when length(col) > 20

  • cl.cex
    Numeric, cex of number-label in color-legend, passed to colorlegend.

  • cl.ratio :
    Numeric, to justify the width of color-legend, 0.1~0.2 is suggested.

  • cl.align.text
    Character, ‘l’, ‘c’ (default) or ‘r’, for number-label in color-legend, ‘l’ means left, ‘c’ means center, and ‘r’ means right.

  • cl.offset
    Numeric, for number-label in color-legend, see text.

  • number.cex :
    The cex parameter to send to the call to text when writing the correlation coefficients into the plot.

  • number.font
    the font parameter to send to the call to text when writing the correlation coefficients into the plot.

  • 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, ‘negative’, ‘positive’ or ‘all’, only valid when method is ‘shade’. If ‘all’, all correlation coefficients’ glyph will be shaded; if ‘positive’, only the positive will be shaded; if ‘negative’, only the negative will be shaded. Note: the angle of shade line is different, 45 degrees for positive and 135 degrees for negative.

  • shade.lwd
    Numeric, the line width of shade.

  • shade.col
    The color of shade line.

  • p.mat
    Matrix of p-value, if NULL, parameter sig.level, insig, pch, pch.col, pch.cex are invalid.

  • sig.level
    Significant level, if the p-value in p-mat is bigger than sig.level, then the corresponding correlation coefficient is regarded as insignificant. If insig is ‘label_sig’, this may be an increasing vector of significance levels, in which case pch will be used once for the highest p-value interval and multiple times (e.g. ‘’, '’, '’) for each lower p-value interval.

  • insig
    Character, specialized insignificant correlation coefficients, ‘pch’ (default), ‘p-value’, ‘blank’, ‘n’, or ‘label_sig’. If ‘blank’, wipe away the corresponding glyphs; if ‘p-value’, add p-values the corresponding glyphs; if ‘pch’, add characters (see pch for details) on corresponding glyphs; if ‘n’, don’t take any measures; if ‘label_sig’, mark significant correlations with pch (see sig.level).

  • pch
    Add character on the glyphs of insignificant correlation coefficients(only valid when insig is ‘pch’). See par.

  • pch.col
    The color of pch (only valid when insig is ‘pch’).

  • pch.cex
    The cex of pch (only valid when insig is ‘pch’).

  • plotCI
    Character, method of ploting confidence interval. If ‘n’, don’t plot confidence interval. If ‘rect’, plot rectangles whose upper side means upper bound and lower side means lower bound, respectively. If ‘circle’, first plot a circle with the bigger absolute bound, and then plot the smaller. Warning: if the two bounds are the same sign, the smaller circle will be wiped away, thus forming a ring. Method ‘square’ is similar to ‘circle’.

  • 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 cells. Default is ‘?’. If ‘square’, then the cell is rendered as a square with the na.label.col color.

  • na.label.col
    Color used for rendering NA cells. Default is ‘black’.

  • 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 text for drawing text label.

实例:

library(corrplot)
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', col.lim = c(0,1))
corrplot(abs(M), order = 'AOE', col = col1(20), 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=col0(10))
corrplot(abs(M)/10+0.5, is.corr = FALSE, col.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, col.lim = c(-100, 100))

## visualize a  matrix in [100, 300]
ran2 = ran + 200

# bad color
corrplot(ran2, is.corr = FALSE, col.lim = c(100, 300), col=col1(100))

# good color
corrplot(ran2, is.corr = FALSE, col.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 = 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

部分结果如下:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

corrplot.mixed()

该函数实现了矩阵上下三角阵不同的图形模式

用法

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, ‘lt’, ‘d’ or ‘n’, giving position of text labels, ‘lt’ means left and top, ‘d’ means diagonal. If ‘n’, add no textlabel.

  • diag
    Character, for specifying the glyph on the principal diagonal. It is one of ‘n’ (default, draw nothing), ‘l’ (draw the glyphs of lower triangular) or ‘u’ (draw the glyphs of upper triangular).

  • bg
    The background color.

  • addgrid.col
    See the addgrid.col parameter in the function corrplot

  • lower.col
    Passed as col parameter to the lower matrix.

  • upper.col
    Passed as col parameter to the upper matrix.

  • plotCI
    See the plotCI parameter in the function corrplot

  • mar
    See par.

实例

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')

结果如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值