文章目录
# 计算相关系数
res <- cor(mydata)
round(res, 2)#保留两位小数
# 计算相关系数,加显著性
library(Hmisc)#加载包
res2 <- rcorr(as.matrix(mydata))
res2
> round(res, 2)#保留两位小数
PM25 AOD BLH PS PM25M RH1000 RH500 TEM1000 TEM500 U1000 U500 V1000 V500 DEM
PM25 1.00 0.27 -0.26 0.24 0.60 -0.32 -0.26 -0.55 -0.49 0.08 0.45 -0.18 -0.34 -0.19
AOD 0.27 1.00 -0.13 0.50 0.76 0.25 -0.21 0.07 0.20 -0.34 0.16 -0.03 0.19 -0.49
BLH -0.26 -0.13 1.00 -0.17 -0.22 -0.44 0.03 0.30 0.06 0.17 -0.18 0.11 -0.14 0.16
PS 0.24 0.50 -0.17 1.00 0.49 0.14 -0.19 -0.24 -0.08 -0.21 0.21 -0.20 0.02 -0.98
PM25M 0.60 0.76 -0.22 0.49 1.00 -0.06 -0.26 -0.20 -0.08 -0.18 0.32 -0.16 -0.03 -0.45
RH1000 -0.32 0.25 -0.44 0.14 -0.06 1.00 0.16 0.35 0.62 -0.44 -0.28 0.04 0.68 -0.18
RH500 -0.26 -0.21 0.03 -0.19 -0.26 0.16 1.00 0.24 0.13 0.17 -0.52 0.26 0.04 0.16
TEM1000 -0.55 0.07 0.30 -0.24 -0.20 0.35 0.24 1.00 0.89 -0.29 -0.71 0.34 0.52 0.16
TEM500 -0.49 0.20 0.06 -0.08 -0.08 0.62 0.13 0.89 1.00 -0.51 -0.56 0.13 0.72 0.02
U1000 0.08 -0.34 0.17 -0.21 -0.18 -0.44 0.17 -0.29 -0.51 1.00 0.07 0.22 -0.52 0.20
U500 0.45 0.16 -0.18 0.21 0.32 -0.28 -0.52 -0.71 -0.56 0.07 1.00 -0.37 -0.22 -0.14
V1000 -0.18 -0.03 0.11 -0.20 -0.16 0.04 0.26 0.34 0.13 0.22 -0.37 1.00 -0.05 0.15
V500 -0.34 0.19 -0.14 0.02 -0.03 0.68 0.04 0.52 0.72 -0.52 -0.22 -0.05 1.00 -0.04
DEM -0.19 -0.49 0.16 -0.98 -0.45 -0.18 0.16 0.16 0.02 0.20 -0.14 0.15 -0.04 1.00
>
> res2
PM25 AOD BLH PS PM25M RH1000 RH500 TEM1000 TEM500 U1000 U500 V1000 V500 DEM
PM25 1.00 0.27 -0.26 0.24 0.60 -0.32 -0.26 -0.55 -0.49 0.08 0.45 -0.18 -0.34 -0.19
AOD 0.27 1.00 -0.13 0.50 0.76 0.25 -0.21 0.07 0.20 -0.34 0.16 -0.03 0.19 -0.49
BLH -0.26 -0.13 1.00 -0.17 -0.22 -0.44 0.03 0.30 0.06 0.17 -0.18 0.11 -0.14 0.16
PS 0.24 0.50 -0.17 1.00 0.49 0.14 -0.19 -0.24 -0.08 -0.21 0.21 -0.20 0.02 -0.98
PM25M 0.60 0.76 -0.22 0.49 1.00 -0.06 -0.26 -0.20 -0.08 -0.18 0.32 -0.16 -0.03 -0.45
RH1000 -0.32 0.25 -0.44 0.14 -0.06 1.00 0.16 0.35 0.62 -0.44 -0.28 0.04 0.68 -0.18
RH500 -0.26 -0.21 0.03 -0.19 -0.26 0.16 1.00 0.24 0.13 0.17 -0.52 0.26 0.04 0.16
TEM1000 -0.55 0.07 0.30 -0.24 -0.20 0.35 0.24 1.00 0.89 -0.29 -0.71 0.34 0.52 0.16
TEM500 -0.49 0.20 0.06 -0.08 -0.08 0.62 0.13 0.89 1.00 -0.51 -0.56 0.13 0.72 0.02
U1000 0.08 -0.34 0.17 -0.21 -0.18 -0.44 0.17 -0.29 -0.51 1.00 0.07 0.22 -0.52 0.20
U500 0.45 0.16 -0.18 0.21 0.32 -0.28 -0.52 -0.71 -0.56 0.07 1.00 -0.37 -0.22 -0.14
V1000 -0.18 -0.03 0.11 -0.20 -0.16 0.04 0.26 0.34 0.13 0.22 -0.37 1.00 -0.05 0.15
V500 -0.34 0.19 -0.14 0.02 -0.03 0.68 0.04 0.52 0.72 -0.52 -0.22 -0.05 1.00 -0.04
DEM -0.19 -0.49 0.16 -0.98 -0.45 -0.18 0.16 0.16 0.02 0.20 -0.14 0.15 -0.04 1.00
n= 33050
P
PM25 AOD BLH PS PM25M RH1000 RH500 TEM1000 TEM500 U1000 U500 V1000 V500 DEM
PM25 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00
AOD 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00
BLH 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00
PS 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 7e-04 0e+00
PM25M 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00
RH1000 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00
RH500 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00
TEM1000 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00
TEM500 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00
U1000 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00
U500 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00
V1000 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00
V500 0e+00 0e+00 0e+00 7e-04 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00
DEM 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00 0e+00
>
corrplot用于可视化相关矩阵,包括一些细节:color, text labels, color labels, layout, etc.
Usage
corrplot(corr, method = c("circle", "square", "ellipse", "number", "shade",
"color", "pie"), type = c("full", "lower", "upper"), add = FALSE,
col = 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.lim = 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, ...)
##Arguments
- corr
The correlation matrix to visualize, must be square if order is not “original”. For general matrix, please using is.corr = FALSE to convert. - method
Character, the visualization method of correlation matrix to be used. Currently, it supports seven methods, named “circle” (default), “square”, “ellipse”, “number”, “pie”, “shade” and “color”. See examples for details.
The areas of circles or squares show the absolute value of corresponding correlation coefficients. Method “pie” and “shade” came from Michael Friendly’s job (with some adjustment about the shade added on), and “ellipse” came from D.J. Murdoch and E.D. Chow’s job, see in section References. - type
Character, “full” (default), “upper” or “lower”, display full matrix, lower triangular or upper triangular matrix. - add
Logical, if TRUE, the graph is added to an existing plot, otherwise a new plot is created. - col
Vector, the color of glyphs. It is distributed uniformly in cl.lim. If NULL, col will be colorRampPalette(col2)(200), see example about col2. - 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 is.corr = FALSE. - 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 outline is TRUE, the default value is “black”. - mar :具体设置图形的四边间距
See par. - addgrid.col
The color of the grid. If NA, don’t add grid. If NULL the default value is chosen. The default value depends on method, if method is color or shade, the color of the grid is NA, that is, not draw grid; otherwise “grey”. - addCoef.col
Color of coefficients added on the graph. If NULL (default), add no coefficients. - addCoefasPercent
Logic, whether translate coefficients into percentage style for spacesaving. - order
Character, the ordering method of the correlation matrix.- “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
Character, the agglomeration method to be used when order is hclust. This should be one of “ward”, “ward.D”, “ward.D2”, “single”, “complete”, “average”, “mcquitty”, “median” or “centroid”. - addrect
Integer, the number of rectangles draws on the graph according to the hierarchical cluster, only valid when order is hclust. If NULL (default), then add no rectangles. - rect.col
Color for rectangle border(s), only valid when addrect is equal or greater than 1. - rect.lwd
Numeric, line width for borders for rectangle border(s), only valid when addrect is equal or greater than 1. - tl.pos : position of text labels
Character or logical, position of text labels. If character, it must be one of “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 textlabel. - tl.cex : the size of text label :指定文本标签的大小
Numeric, for the size of text label (variable names). - tl.col : color of text label. 指定文本标签的颜色
The color of text label. - tl.offset
Numeric, for text label, see text. - tl.srt : text label string rotation
Numeric, for text label string rotation in degrees, see text. - cl.pos : position of color labels 颜色带
Character or logical, position of color labels; If character, it must be one of “r” (default if type==“upper” or “full”), “b” (default if type==“lower”) or “n”, “n” means don’t draw colorlabel. - cl.lim
The limits (x1, x2) in the colorlabel. - cl.length
Integer, the number of number-text in colorlabel, 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 colorlabel, passed to colorlegend. - cl.ratio
Numeric, to justify the width of colorlabel, 0.1~0.2 is suggested. - cl.align.text
Character, “l”, “c” (default) or “r”, for number-label in colorlabel, “l” means left, “c” means center, and “r” means right. - cl.offset
Numeric, for number-label in colorlabel, 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, arguments sig.level, insig, pch, pch.col, pch.cex is 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 : confidence interval部分
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, and meanwhile correlation coefficients are also added on the rectangles. 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 lable.
1 method: “circle”, “square”, “ellipse”, “number”, “shade”, “color”, “pie”.
library(corrplot)
M <- cor(mtcars)
corrplot(M, method = "circle")
corrplot(M, method = "square")
corrplot(M, method = "number") # Display the correlation coefficient
corrplot(M, method = "pie")
2. type type = c(“full”, “lower”, “upper”)
corrplot(M, type = "upper")
3. corrplot.mixed() is a wrapped function for mixed visualization style.
Usage
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), ...)
Arguments
- corr
Matrix, the correlation matrix to visualize. - lower 右下角的可视化方法method
Character, the visualization method for the lower triangular correlation matrix. - upper
Character, the visualization method for the upper triangular correlation matrix. - tl.pos : position of text labels 位置
Character, “lt”, “d” or “n”, giving position of text labels, “lt” means left and top, “d” means diagonal. If “n”, add no textlabel. - diag “n”:空, “l” :对角线都是1, “u”:对角线是符号
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. - …
Additional arguments for corrplot’s wrappers
corrplot.mixed(M)
示例:
library("corrplot")
M <- cor(mtcars)
ord <- corrMatOrder(M, order = "AOE")
M2 <- M[ord,ord]
round(M,2)
round(M2,2)
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")
corrplot.mixed(M2, tl.pos = “lt”, diag = “u”)
corrplot.mixed(M2, tl.pos = “lt”, diag = “l”)
corrplot.mixed(M2, tl.pos = “lt”)
corrplot.mixed(M2, tl.pos = “n”)
参考文章:
https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html