corrplot的基础用法和指南
corrplot包主要针对相关系数输出的结果进行可视化,可以实现选择颜色,文本标签,颜色标签,布局等的操作。
语法和常用参数介绍
函数语法
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", "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,
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"),
pch = 4, pch.col = "black", pch.cex = 3,
plotCI = c("n","square", "circle", "rect"),
lowCI.mat = NULL, uppCI.mat = NULL, ...)
参数介绍
corr:需要可视化的相关系数矩阵
method:指定可视化的方法,可以是圆形、方形、椭圆形、数值、阴影、颜色或饼图形
type:指定展示的方式,可以是完全的、下三角或上三角(“full”,“upper"和"lower”)
col:指定图形展示的颜色,默认以均匀的颜色展示
bg:指定图的背景色
title:为图形添加标题
is.corr:是否为相关系数绘图,默认为TRUE,同样也可以实现非相关系数的可视化,只需使该参数设为FALSE即可
diag:是否展示对角线上的结果,默认为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时,图例在右表®,当type=lower时,图例在底部,不需要图例时,只需指定该参数为n
addshade:只有当method=shade时,该参数才有用,参数值可以是negtive/positive和all,分表表示对负相关系数、正相关系数和所有相关系数添加阴影。注意:正相关系数的阴影是45度,负相关系数的阴影是135度
shade.lwd:指定阴影的线宽
shade.col:指定阴影线的颜色
ggcorrplot的基础用法指南
ggcorrplot提供了一种对相关矩阵进行重新排序的解决方案,并在相关图上显示显著性水平。它还包括一个用于计算相关p值矩阵的函数。
安装和加载
install.packages(“ggcorrplot”)
library(ggcorrplot)
#可视化相关性矩阵
ggcorrplot(corr)#method默认为square,即方形
#method = “circle” 圆形
ggcorrplot(corr, method = “circle”)
#重排矩阵,使用分层聚类
ggcorrplot(corr, hc.order = TRUE, outline.color = “white”)
#展示下半三角
ggcorrplot(corr,
hc.order = TRUE,
type = “lower”,
outline.color = “white”)
#展示上半三角
ggcorrplot(corr,
hc.order = TRUE,
type = “upper”,
outline.color = “white”)
#改变主题和颜色
ggcorrplot(
corr,
hc.order = TRUE,
type = “lower”,
outline.color = “white”,
ggtheme = ggplot2::theme_gray,
colors = c("#6D9EC1", “white”, “#E46726”)
)
#加上相关性系数标签
ggcorrplot(corr,
hc.order = TRUE,
type = “lower”,
lab = TRUE)
#给有显著性差异的标上记
ggcorrplot(corr,
hc.order = TRUE,
type = “lower”,
p.mat = p.mat)
#在没有显著系数的地方留空
ggcorrplot(
corr,
p.mat = p.mat,
hc.order = TRUE,
type = “lower”,
insig = “blank”
)