R语言绘图包系列:
- R语言绘图包01--优秀的拼图包patchwork
- R语言绘图包02--热图pheatmap
- R语言绘图包03--火山图EnhancedVolcano
- R语言绘图包04--GOplot:富集分析结果可视化
- R语言绘图包05--韦恩图的绘制:ggvenn和VennDiagram
1. 安装
install.packages('corrplot')
2. 用法和参数
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"),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, ...)
参数
参数(常用) | 用法 |
---|---|
corr | 用于绘图的矩阵,必须是正方形矩阵,如果是普通的矩阵,需要设置is.corr=FALSE |
method | 可视化的方法,默认是圆circle,还有正方形square、椭圆ellipse、数字number、阴影shade、颜色color和饼pie可选 |
type | 展示类型,默认全显full,还有下三角lower,或上三角upper可选 |
col | 指定图形展示的颜色,默认以均匀的颜色展示 |
bg | 指定图的背景色 |
title | 为图形添加标题 |
is.corr | 逻辑值,是否为相关系数绘图,默认为TRUE,同样也可以实现非相关系数的可视化,只需使该参数设为FALSE |
diag | 是否显示对角线值。因为对角线全为1,显示只是美观,实际意义不大,默认为TRUE |
outline | 是否绘制圆形、方形或椭圆形的轮廓,默认为FALSE |
mar | 具体设置图形的四边间距 |
addgrid.col | 当选择的方法为颜色或阴影时,默认的网格线颜色为白色,否则为灰色 |
addCoef.col | 为相关系数添加颜色,默认不添加相关系数,只有方法为number时,该参数才起作用 |
addCoefasPercent | 为节省绘图空间,是否将相关系数转换为百分比格式,默认为FALSE |
order | 指定相关系数排序的方法,可以是原始顺序(original)、特征向量角序(AOE)、第一主成分顺序(FPC)、层次聚类顺序(hclust)和字母顺序,一般”AOE”排序结果都比”FPC”要好。 |
hclust.method | 当order为hclust时,该参数可以是层次聚类中ward法、最大距离法等7种之一 |
addrect | 当order为hclust时,可以为添加相关系数图添加矩形框,默认不添加框,如果想添加框时,只需为该参数指定一个整数即可 |
rect.col | 指定矩形框的颜色 |
rect.lwd | 指定矩形框的线宽 |
tl.pos | 指定文本标签(变量名称)的位置,当type=full时,默认标签位置在左边和顶部(lt),当type=lower时,默认标签在左边和对角线(ld),当type=upper时,默认标签在顶部和对角线,d表示对角线,n表示不添加文本标签 |
tl.cex | 指定文本标签的大小 |
tl.col | 指定文本标签的颜色 |
cl.pos | 图例(颜色)位置,当type=upper或full时,图例在右表(r),当type=lower时,图例在底部,不需要图例时,只需指定该参数为n |
addshade | 只有当method=shade时,该参数才有用,参数值可以是negtive/positive和all,分表表示对负相关系数、正相关系数和所有相关系数添加阴影。注意:正相关系数的阴影是45度,负相关系数的阴影是135度 |
shade.lwd | 指定阴影的线宽 |
shade.col | 指定阴影线的颜色 |
p.mat | p值的矩阵,如果为NULL,则参数sig.level,insig,pch,pch.col,pch.cex均无效 |
sig.level | 显着水平,如果p-mat中的p值大于sig.level,那么相应的相关系数被认为是无关紧要的。如果insig是“label_sig”,这种情况下,用“ * "(例如“ * ",“ ** ",“ *** " )来强调显著性 |
plotCI | 字符,绘制置信区间的方法。如果为“n”,则不绘制置信区间。如果是“rect”,则绘制矩形,其上侧分别表示上限,下侧表示下限,同时相关系数也加在矩形上。如果是“circle',首先绘制一个具有较大绝对界限的圆,然后绘制较小的圆 |
lowCI.mat | 置信区间下限的矩阵 |
uppCI.mat | 置信区间上限的矩阵 |
3. 演示
#加载corrplot包
library(corrplot)
library(patchwork)
#示例使用R内置数据mtcars
head(mtcars)
# mpg cyl disp hp drat wt qsec vs am gear carb
# Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
# Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
# Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
# Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
# Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
# Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
M <- cor(mtcars) #先求相关系数
head(M)
# mpg cyl disp hp drat wt qsec vs am gear carb
# mpg 1.0000000 -0.8521620 -0.8475514 -0.7761684 0.6811719 -0.8676594 0.41868403 0.6640389 0.5998324 0.4802848 -0.5509251
# cyl -0.8521620 1.0000000 0.9020329 0.8324475 -0.6999381 0.7824958 -0.59124207 -0.8108118 -0.5226070 -0.4926866 0.5269883
# disp -0.8475514 0.9020329 1.0000000 0.7909486 -0.7102139 0.8879799 -0.43369788 -0.7104159 -0.5912270 -0.5555692 0.3949769
# hp -0.7761684 0.8324475 0.7909486 1.0000000 -0.4487591 0.6587479 -0.70822339 -0.7230967 -0.2432043 -0.1257043 0.7498125
# drat 0.6811719 -0.6999381 -0.7102139 -0.4487591 1.0000000 -0.7124406 0.09120476 0.4402785 0.7127111 0.6996101 -0.0907898
# wt -0.8676594 0.7824958 0.8879799 0.6587479 -0.7124406 1.0000000 -0.17471588 -0.5549157 -0.6924953 -0.5832870 0.4276059
- 3.1 首先使用默认参数绘图
corrplot(M)
- 3.2 设置
可视化方法(method)
和展示类型(type)
par(mfrow=c(1,2))
corrplot(M,method='ellipse') #更改可视化方法method(7种可选)
corrplot(M, type = 'upper') #更改展示类型type(默认full,可选上三角upper、下三角lower)
- 3.3 设置
颜色(color)
par(mfrow=c(1,2))
#直接设置颜色
wb <- c('blue', 'black','red')
corrplot(M, col = wb)
#颜色设置,还可以使用colorRampPalette颜色梯度函数(色彩更丰富)
col3 <- colorRampPalette(c("blue", "black", "red"))
corrplot(M, col = col3(20)) #20表示 20个色阶
- 3.4 设置背景色(bg)
# bg设置背景色,cl.pos = "n"不展示颜色图例
corrplot(M, bg = "grey", order = "AOE", cl.pos = "n")
- 3.5 用hclust方法聚类
par(mfrow=c(1,2))
## addrect设置分组变量,添加矩形
## hclust.method = "ward.D2"设定聚类方法
corrplot(M, order = "hclust", addrect = 2,rect.col = "black")
corrplot(M, order = "hclust", hclust.method = "ward.D2", addrect = 4,rect.col = "blue")
- 3.6 设定不同展示顺序(order)
addCoef.col添加相关系数并设定颜色
corrplot(M, order = "AOE", addCoef.col = "grey")
- 3.7 tl.pos = "n"不展示文本标签,tl.srt参数设定文本标签摆放角度
diag = FALSE不展示对角线的相关系数
par(mfrow=c(1,2))
corrplot(M, order = "AOE", type = "lower", method = "ellipse",
diag = FALSE, tl.pos = "n", cl.pos = "n")
corrplot(M, order = "AOE", tl.srt = 45)
- 3.8 修改图例范围 abs(M):abs取绝对值;cl.lim设定图例颜色范围,cl.pos设定颜色图例的位置。
par(mfrow=c(1,2))
corrplot(abs(M), order = "AOE", cl.lim = c(0,1))
corrplot(M, order = "AOE", cl.pos = "b")
- 3.9 显著性标记
par(mfrow=c(2,3))
## 设置没通过统计的相关性结果打X,conf.level置信水平
res1 <- cor.mtest(mtcars, conf.level = .95)
corrplot(M, p.mat = res1$p, sig.level = .01)
## 设置不显著的空白
corrplot(M, p.mat = res1$p, insig = "blank")
## 设置不显著的显示p值
corrplot(M, p.mat = res1$p, insig = "p-value")
## 显示所有p值
corrplot(M, p.mat = res1$p, insig = "p-value", sig.level = -1)
## 用 * 数量代表显示性
corrplot(M, p.mat = res1$p, insig = "label_sig",
sig.level = c(.001, .01, .05), pch.cex = .9, pch.col = "black")
- 3.10 画置信区间
plotCI=circle,可选择:square、rect,n为不绘制
par(mfrow=c(1,3))
corrplot(M, low = res1$uppCI, upp = res1$uppCI,
plotCI = "circle", addg = "grey20", cl.pos = "n")
#置信区间与相关性同时表示,不显著标记x
corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
plotCI = "circle", addg = "grey20", cl.pos = "n")
#不显著标记X,颜色更换
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")
- 3.11 在已有的图形上添加其他图形add = TRUE
如果用corrplot.mixed()函数更高效
par(mfrow=c(1,2))
corrplot(M)
corrplot(M, add = TRUE, type = "lower", method = "ellipse", order = "AOE",
diag = FALSE, tl.pos = "n", cl.pos = "n")
#add,更复杂举例
A<-corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
plotCI = "circle", addg = "grey20", cl.pos = "n")
corrplot(A, add = TRUE, type = "upper", method = "number",
order = "AOE", diag = FALSE, tl.pos = "n", cl.pos = "n")
参考:
https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html
https://mp.weixin.qq.com/s/zq5jiOTeAM1dxfqnthWBBg