简介:
相关图是基于相关系数矩阵绘制的图。
通常是将1个变量映射到多个视觉元素,所以看起来很花哨。
如果是椭圆:
则椭圆的色相对应相关性的正负,
颜色深浅对应相关性绝对值大小,越深则绝对值越大。
椭圆的形状对应相关性绝对值大小,默认越扁,则相关性绝对值越大。
如果是圆,则圆的面积对应相关性大小,
如果是扇形,则扇形的弧度对应相关性大小。
相关系数:
自变量X和因变量Y的协方差/标准差的乘积。也可以反映两个变量变化时是同向还是反向,
如果同向变化就为正,反向变化就为负。
它消除了两个变量变化幅度的影响,而只是单纯反应两个变量每单位变化时的相似程度。
表达式: cor(x, y = NULL, use = "everything", method = c("pearson", "kendall", "spearman"))
参数解释:
x 为数字型向量,矩阵或数据框,表示自变量
y 表示应变量,默认y=x
2个向量计算得到一个值,n个变量组成的数据框计算得到长度为n*n维度的矩阵。
绘制相关图主要涉及2个包:corrplot
, ggcorrplot
,后一个是ggplot2
的扩展包。
计算相关系数矩阵:
1height <- c(6, 5.92, 5.58, 5.83) 2wei <- c(20, 15, 7, 12) 3cor(height, exp(height)) 4cor(height, wei) 5ncol(mtcars) 6dim(cor(mtcars)) # 7class(cor(mtcars)) 8colnames(cor(mtcars)) 9row.names(cor(mtcars))1011# 展示系数矩阵,保留3位小数,12DT::datatable(round(cor(mtcars), 3), 13 options = list(pageLength = 11)) # 显示11行
1## [1] 0.99830742## [1] 0.96288113## [1] 114## [1] 11 115## [1] "matrix"6## [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"7## [11] "carb"8## [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"9## [11] "carb"
(原图可交互)
corrplot包绘图:
结果按行和按列排是一样的,说明,只要cor(x,y)
中,只要x=y
,按行排和按列排没有区别。
1library(corrplot)2corrplot(cor(mtcars))
1library(ggplot2)2library(ggcorrplot)34ggcorrplot(cor(mtcars), method="circle")
1.corrplot包
1.1
语法与参数
语法:
1corrplot(corr, 2 method = c("circle", "square", "ellipse", "number", "shade", "color", "pie"), 3 type = c("full", "lower", "upper"), add = FALSE, 4 col = NULL, bg = "white", title = "", is.corr = TRUE, 5 diag = TRUE, outline = FALSE, mar = c(0,0,0,0), 6 addgrid.col = NULL, addCoef.col = NULL, addCoefasPercent = FALSE, 7 order = c("original", "AOE", "FPC", "hclust", "alphabet"), 8 hclust.method = c("complete", "ward", "single", "average", 9 "mcquitty", "median", "centroid"),10 addrect = NULL, rect.col = "black", rect.lwd = 2,11 tl.pos = NULL, tl.cex = 1,12 tl.col = "red", tl.offset = 0.4, tl.srt = 90,13 cl.pos = NULL, cl.lim = NULL,14 cl.length = NULL, cl.cex = 0.8, cl.ratio = 0.15, 15 cl.align.text = "c",cl.offset = 0.5,16 addshade = c("negative", "positive", "all"),17 shade.lwd = 1, shade.col = "white",18 p.mat = NULL, sig.level = 0.05,19 insig = c("pch","p-value","blank", "n"),20 pch = 4, pch.col = "black", pch.cex = 3,21 plotCI = c("n","square", "circle", "rect"),22 lowCI.mat = NULL, uppCI.mat = NULL, ...)
关键参数:
corr
, 需要可视化的相关系数矩阵,method
, 指定可视化的形状,可以是circle
圆形(默认),square
方形,ellipse
, 椭圆形,number
数值,shade
阴影,color
颜色,pie
饼图。type
,指定显示范围,可以是full
完全(默认),lower
下三角,upper
上三角。col
, 指定图形展示的颜色,默认以均匀的颜色展示。
支持grDevices
包中的调色板,也支持RColorBrewer
包中调色板。bg
, 指定背景颜色。add
, 表示是否添加到已经存在的plot中。默认FALSE
生成新plot。title
, 指定标题,is.corr
,是否为相关系数绘图,默认为TRUE
,FALSE
则可将其它数字矩阵进行可视化。diag
, 是否展示对角线上的结果,默认为TRUE
,outline
, 是否添加圆形、方形或椭圆形的外边框,默认为FALSE
。mar
, 设置图形的四边间距。数字分别对应(bottom, left, top, right)
。addgrid.col
, 设置网格线颜色,当指定method
参数为color
或shade
时, 默认的网格线颜色为白色,其它method
则默认为灰色,也可以自定义颜色。addCoef.col
, 设置相关系数值的颜色,只有当method
不是number
时才有效。addCoefasPercent
, 是否将相关系数转化为百分比形式,以节省空间,默认为FALSE
。order
, 指定相关系数排序的方法, 可以是original
原始顺序,AOE
特征向量角序,FPC
第一主成分顺序,hclust
层次聚类顺序,alphabet
字母顺序。hclust.method
, 指定hclust
中细分的方法,只有当指定order
参数为hclust
时有效,
有7种可选:complete
,ward
,single
,average
,mcquitty
,median
,centroid
。addrect
, 是否添加矩形框,只有当指定order
参数为hclust
时有效, 默认不添加, 用整数指定即可添加。rect.col
, 指定矩形框的颜色。rect.lwd
, 指定矩形框的线宽。tl.pos
, 指定文本标签(变量名称)相对绘图区域的位置,为"lt"
(左侧和顶部),"ld"
(左侧和对角线),"td"
(顶部和对角线),"d"
(对角线),"n"
(无)之一。当
type="full"
时,默认"lt"
。当
type="lower"
时,默认"ld"
。当
type="upper"
时,默认"td"
。tl.cex
, 设置文本标签的大小。tl.col
, 设置文本标签的颜色。cl.pos
, 设置图例位置,为"r"
(右边),"b"
(底部),"n"
(无)之一。
当type="full"/"upper"
时,默认"r"
; 当type="lower"
时,默认"b"
。addshade
, 表示给增加阴影,只有当method="shade"
时有效。
为"negative"
(对负相关系数增加阴影),负相关系数的阴影是135度;"positive"
(对正相关系数增加阴影), 正相关系数的阴影是45度;"all"
(对所有相关系数增加阴影),之一。shade.lwd
, 指定阴影线宽。shade.col
, 指定阴影线的颜色。
1.2
method与type
1library(corrplot) 2library(showtext) 3mat_cor <- cor(mtcars) 4 5par(mfrow = c(2,2)) # 多图排版,2x2矩阵排列 6 7corrplot(mat_cor, title = "默认圆形全显示", # 默认method为圆形,默认type为full 8 mar = c(1,1,1,1)) # 指定边距,否则标题显示不完全 9corrplot(mat_cor, method = "ellipse", type = "upper", title = "椭圆上三角",10 mar = c(1,1,1,1))11corrplot(mat_cor, method = "number", type = "lower", title = "数字下三角",12 mar = c(1,1,1,1))13corrplot(mat_cor, method = "circle", type = "upper", title = "圆形上三角",14 mar = c(1,1,1,1))15corrplot(mat_cor, method = "square", type = "lower", title = "方形下三角",16 mar = c(1,1,1,1))17corrplot(mat_cor, method = "shade", type = "full", title = "阴影全显示",18 mar = c(1,1,1,1))19corrplot(mat_cor, method = "color", type = "upper", title = "颜色上三角",20 mar = c(1,1,1,1))21corrplot(mat_cor, method = "pie", type = "lower", title = "饼图下三角",22 mar = c(1,1,1,1))
1.3
col颜色
颜色可以自定义,支持grDevices
包中的调色板。也支持RColorBrewer
中的调色板。
1# 自定义色板 2color_1 <- colorRampPalette(c("cyan", "magenta")) 3color_2 <- colorRampPalette(c("magenta", "cyan")) # 色板反向 4palette_1 <- RColorBrewer::brewer.pal(n=11, name = "RdYlGn") 5palette_2 <- rev(palette_1) # 色板反向 6 7par(mfrow = c(2, 2)) 8 9corrplot(mat_cor, method = "number", col = "black", cl.pos = "n", 10 title = "黑色数字", mar = c(1,1,1,1))1112corrplot(mat_cor, method = "ellipse", col = color_1(10), 13 title = "自定义颜色", mar = c(1,1,1,1))1415corrplot(mat_cor, method = "ellipse", col = color_1(200), # 矩阵维度不够大,所以颜色没区别16 title = "自定义颜色", mar = c(1,1,1,1))1718corrplot(mat_cor, method = "ellipse", col = color_2(10), 19 title = "色板反向", mar = c(1,1,1,1))2021par(mfrow = c(1,1))22corrplot(mat_cor, method = "ellipse", col = palette_1,23 title = "brewer.pal调色板", mar = c(1,1,1,1))24corrplot(mat_cor, method = "ellipse", col = palette_2,25 title = "色板反向", mar = c(1,1,1,1)) 26
1.4
diag和bg
1corrplot(mat_cor, method = "ellipse", type = "lower", col = palette_2,2 title = "默认显示对角线",diag = TRUE, mar = c(1,1,1,1))3corrplot(mat_cor, method = "ellipse", type = "lower", col = palette_2,4 title = "不显示对角线", diag = FALSE, mar = c(1,1,1,1))5corrplot(mat_cor, method = "ellipse", type = "lower", col = palette_2,6 title = "灰色背景", bg = "gray60", mar = c(1,1,1,1))7corrplot(mat_cor, method = "ellipse", type = "lower", col = palette_2,8 title = "浅绿背景", bg = "lightblue", mar = c(1,1,1,1))
1.5
order顺序
1corrplot(mat_cor, method = "ellipse", col = palette_2, 2 title = "默认original顺序", diag = TRUE, mar = c(1,1,1,1)) 3corrplot(mat_cor, method = "ellipse", order = "AOE", col = palette_2, 4 title = "AOE特征向量角序", diag = TRUE, mar = c(1,1,1,1)) 5corrplot(mat_cor, method = "ellipse", order = "FPC", col = palette_2, 6 title = "FPC第一主成分顺序", diag = TRUE, mar = c(1,1,1,1)) 7corrplot(mat_cor, method = "ellipse", order = "hclust", col = palette_2, 8 title = "hclust层次聚类顺序", diag = TRUE, mar = c(1,1,1,1)) 9corrplot(mat_cor, method = "ellipse", order = "alphabet", col = palette_2,10 title = "alphabet字母顺序", diag = TRUE, mar = c(1,1,1,1))
1.6
hclust.method和addrect
只有当order="hclust"
才有效。
1corrplot(mat_cor, method = "ellipse", order = "hclust", col = palette_2, 2 hclust.method = "complete", addrect = 1, rect.col = "blue", rect.lwd = 2, 3 title = "hclust.method = \"complete\"", diag = TRUE, mar = c(1,1,1,1)) 4corrplot(mat_cor, method = "ellipse", order = "hclust", col = palette_2, 5 hclust.method = "ward", addrect = 2, rect.col = "blue", rect.lwd = 2, 6 title = "hclust.method = \"ward\"", diag = TRUE, mar = c(1,1,1,1)) 7corrplot(mat_cor, method = "ellipse", order = "hclust", col = palette_2, 8 hclust.method = "single", addrect = 3, rect.col = "blue", rect.lwd = 2, 9 title = "hclust.method = \"single\"", diag = TRUE, mar = c(1,1,1,1))10corrplot(mat_cor, method = "ellipse", order = "hclust", col = palette_2,11 hclust.method = "average", addrect = 4, rect.col = "blue", rect.lwd = 2,12 title = "hclust.method = \"average\"", diag = TRUE, mar = c(1,1,1,1))13corrplot(mat_cor, method = "ellipse", order = "hclust", col = palette_2,14 hclust.method = "mcquitty", addrect = 2, rect.col = "blue", rect.lwd = 2,15 title = "hclust.method = \"mcquitty\"", diag = TRUE, mar = c(1,1,1,1))16corrplot(mat_cor, method = "ellipse", order = "hclust", col = palette_2,17 hclust.method = "median", addrect = 2, rect.col = "blue", rect.lwd = 2,18 title = "hclust.method = \"median\"", diag = TRUE, mar = c(1,1,1,1))19corrplot(mat_cor, method = "ellipse", order = "hclust", col = palette_2,20 hclust.method = "centroid", addrect = 2, rect.col = "blue", rect.lwd = 2,21 title = "hclust.method = \"centroid\"", diag = TRUE, mar = c(1,1,1,1))
1.7
addCoef.col与addCoefasPercent
1corrplot(mat_cor, method = "ellipse", order = "AOE", col = palette_2, 2 addCoef.col = "blue", 3 title = "添加蓝色系数值", diag = TRUE, mar = c(1,1,1,1)) 4corrplot(mat_cor, method = "ellipse", order = "AOE", col = palette_2, 5 addCoef.col = "gray20", 6 title = "添加灰色系数值", diag = TRUE, mar = c(1,1,1,1)) 7 8corrplot(mat_cor, method = "ellipse", order = "AOE", col = palette_2, 9 addCoef.col = "blue", addCoefasPercent = TRUE,10 title = "添加蓝色百分比系数", diag = TRUE, mar = c(1,1,1,1))11corrplot(mat_cor, method = "ellipse", order = "AOE", col = palette_2, 12 addCoef.col = "gray20", addCoefasPercent = TRUE,13 title = "添加灰色百分比系数", diag = TRUE, mar = c(1,1,1,1))
——————————————
1.8
cl.pos图例位置
1corrplot(mat_cor, method = "ellipse", order= "AOE", col = palette_2,
2addCoef.col = "gray20", addCoefasPercent = TRUE, cl.pos = "r",
3title = "图例在右边", diag = TRUE, mar = c(1,1,1,1))
4corrplot(mat_cor, method = "ellipse", order= "AOE", col = palette_2,
5addCoef.col = "gray20", addCoefasPercent = TRUE, cl.pos = "b",
6title = "图例在底部", diag = TRUE, mar = c(1,1,1,1))
7corrplot(mat_cor, method = "ellipse", order= "AOE", col = palette_2,
8addCoef.col = "gray20", addCoefasPercent = TRUE, cl.pos = "n",
9title = "无图例", diag = TRUE, mar = c(1,1,1,1))
1.9
变量文本tl.pos、tl.cex及tl.col
tl.pos只有在混合布局的时候才有意义。
1corrplot(mat_cor, method = "ellipse", order= "AOE", col = palette_2,
2addCoef.col = "gray20", addCoefasPercent = TRUE, cl.pos = "r",
3tl.pos = "lt",tl.cex = 2, tl.col = "blue",
4title = "蓝色变量文本", diag = TRUE, mar = c(1,1,1,1))
5
6corrplot(mat_cor, method = "ellipse", order= "AOE", col = palette_2,
7addCoef.col = "gray20", addCoefasPercent = TRUE, cl.pos = "r",
8tl.pos = "n",
9title = "无变量文本", diag = TRUE, mar = c(1,1,1,1))
1.10
阴影设置
只有当method="shade"时,该参数才有用。
addshade添加阴影范围,分为正阴影,负阴影,全阴影。
shade.lwd设置阴影线宽。shade.col设置阴影线颜色。
1corrplot(mat_cor, method = "shade", order= "AOE", col = palette_2,
2addshade = "negative", shade.lwd = 1, shade.col = "blue",
3title = "蓝色负阴影", mar = c(1,1,1,1))
4corrplot(mat_cor, method = "shade", order= "AOE", col = palette_2,
5addshade = "positive", shade.lwd = 1, shade.col = "blue",
6title = "蓝色正阴影", mar = c(1,1,1,1))
7corrplot(mat_cor, method = "shade", order= "AOE", col = palette_2,
8shade.lwd = 1, shade.col = "blue",
9title = "默认全阴影", mar = c(1,1,1,1))
1.11
显著性标记sig.level及p.mat
只有指定矩阵的P值,sig.level,pch等参数才有效。
只有当insig = "pch"时,pch.col和pch.cex参数才有效。
对于p值不清楚的同学,可以参考 知乎的答案
(https://www.zhihu.com/question/23149768)
概况起来,就一句话:小于p值的不可能信,没有意义。
1library(corrplot)
2
3res1 <-cor.mtest(mtcars, conf.level= .95)
4
5corrplot(mat_cor, method= "circle", col= palette_2,
6p.mat= res1$p,sig.level= 0.01,
7title= "增加显著性标记", mar= c(1,1,1,1))
8
9corrplot(mat_cor, method= "circle", col= palette_2,
10p.mat= res1$p,sig.level= 0.01,insig= "pch", pch.col= "blue", pch.cex= 3,
11title= "蓝色显著性标记", mar= c(1,1,1,1))
12
1.12
add混合布局
add参数表示是否添加到已经存在的plot中。默认FALSE生成新plot。
1# 第一个图,
2corrplot(mat_cor, method = "ellipse", type = "upper", order= "AOE",
3col = palette_2, tl.pos = "d",
4title = "上椭圆下百分比混合布局", mar = c(1,1,1,1))
5corrplot(mat_cor, method = "number", type = "lower", order= "AOE", col = palette_2,
6add = TRUE, diag = FALSE, tl.pos = "n", addCoefasPercent = TRUE, cl.pos = "n",
7mar = c(1,1,1,1))
8# 第2个图,
9corrplot(mat_cor, method = "pie", type = "lower", order= "AOE",
10col = palette_2, tl.pos = "tp", tl.col = "blue", cl.pos = "r",
11title = "上数字下饼图混合布局", mar = c(1,1,1,1))
12corrplot(mat_cor, method = "number", type = "upper", order= "AOE", col = palette_2,
13add = TRUE, diag = FALSE, tl.pos = "n", cl.pos = "n",
14mar = c(1,1,1,1))
2.ggcorrplot包
ggcorrplot包内就2个函数,一个cor_pmat()用于计算p值, 一个ggcorrplot()用于绘图。
ggcorrplot相当于精简版的corrplot包。只有主题更加丰富多样。
2.1
语法及参数
语法:
1ggcorrplot(corr, method = c("square", "circle"), type = c("full", "lower",
2"upper"), ggtheme = ggplot2::theme_minimal, title = "",
3show.legend = TRUE, legend.title = "Corr", show.diag = FALSE,
4colors = c("blue", "white", "red"), outline.color = "gray",
5hc.order = FALSE, hc.method = "complete", lab = FALSE,
6lab_col = "black", lab_size = 4, p.mat = NULL, sig.level = 0.05,
7insig = c("pch", "blank"), pch = 4, pch.col = "black", pch.cex = 5,
8tl.cex = 12, tl.col = "black", tl.srt = 45, digits = 2)
关键参数:
- method,相比corrplot,少了很多种,只有方形和圆形,默认方形。
- colors,需要长度为3的颜色向量,同时指定low,mid和high处的颜色。
- outline.color,指定方形或圆形的边线颜色。
- hc.order,是否按hclust(层次聚类顺序)排列。
- hc.method,相当于corrplot中的hclust.method, 指定方法一样,详情见?hclust。
- lab, 是否添加相关系数。
- lab_col,指定相关系数的颜色,只有当lab=TRUE时有效。
- lab_size,指定相关系数大小,只有当lab=TRUE时有效。
- show.legend, 是否显示图例。
- legend.title,指定图例标题。
- sig.level,insig,pch,pch.col,pch.cex,与corrplot中完全一样。
- tl.cex, 指定变量文本的大小,
- tl.col, 指定变量文本的颜色,
- tl.srt, 指定变量文本的旋转角度。
- digits, 指定相关系数的显示小数位数(默认2)。
2.2
实例
1library(ggplot2)
2library(ggcorrplot)
3library(showtext)
4
5# 更改字体
6windowsFonts(YaHei_rontine = windowsFont("微软雅黑"),
7Time_NewR = windowsFont("Times New Romans 常规"))
8font_add("YaHei_rontine", regular = "msyh.ttc", bold = "msyhbd.ttc")
9font_add("Time_NewR", "times.ttf",
10bold = "timesbd.ttf",
11italic = "timesi.ttf",
12bolditalic = "timesbd.ttf")
13
14showtext_auto()
15
16# 自定义主题
17mytheme <- theme_bw() +
18theme(
19plot.title = element_text(colour = "blue", hjust = 0.5, size = 20),
20legend.text = element_text(colour = "blue"),
21legend.title = element_text(family = "YaHei_rontine", colour = "blue"),
22legend.position = "bottom", legend.direction = "horizontal"
24
25
26# 绘图
27ggcorrplot(mat_cor,
28method="circle",
29hc.order = TRUE,
30type = "lower",
31lab = TRUE, # 显示相关系数
32lab_col = "blue", lab_size = 3,
33colors = c("cyan", "white", "magenta"),
34tl.cex = 10, tl.col = "blue", digits = 1,
35title="下三角圆形,hclust排列",
36legend.title = "相关系数",
37ggtheme = theme_bw())
38
39# 自定义主题
40ggcorrplot(mat_cor,
41method="circle",
42hc.order = TRUE,
43type = "lower",
44lab = TRUE, # 显示相关系数
45lab_col = "blue", lab_size = 3,
46colors = c("cyan", "white", "magenta"),
47tl.cex = 10, tl.col = "blue", digits = 1,
48title="自定义主题",
49legend.title = "相关系数",
50ggtheme = mytheme)
2.3
显著性标记
1library(ggplot2)
2library(ggcorrplot)
3
4p_mat <-cor_pmat(mtcars)
5
6ggcorrplot(mat_cor,
7method="circle", hc.order= TRUE,type= "lower",
8lab= TRUE,lab_col= "blue", lab_size= 3,# 显示相关系数
9colors= c("cyan", "white", "magenta"),
10tl.cex= 10,tl.col= "blue", digits= 1,
11title="显著性标记",
12legend.title= "相关系数",
13p.mat= p_mat,
14ggtheme= theme_bw())
15
16ggcorrplot(mat_cor,
17hc.order= TRUE,type= "full",
18colors= c("cyan", "white", "magenta"),
19tl.cex= 10,tl.col= "blue", digits= 1,
20title="低于p值为空",
21legend.title= "相关系数",
22p.mat= p_mat,insig= "blank",
23ggtheme= theme_bw())
24
25ggcorrplot(mat_cor,
26method="circle", hc.order= TRUE,type= "upper",
27colors= c("cyan", "white", "magenta"),
28tl.cex= 10,tl.col= "blue", digits= 1,
29title="红色显著性标记",
30legend.title= "相关系数",
31p.mat= p_mat,insig= "pch", pch.col= "red", pch.cex= 4,
32ggtheme= theme_bw())
33
参
考资料返回搜狐,查看更多
- 使用corrplot包绘制相关性图 //www.greatytc.com/p/00000f6f32df
- R画月亮阴晴圆缺:corrplot绘图相关系数矩阵 https://blog.csdn.net/woodcorpse/article/details/79417978
- ggcorrplot: Visualization of a correlation matrix using ggplot2 http://www.sthda.com/english/wiki/ggcorrplot-visualization-of-a-correlation-matrix-using-ggplot2
- R语言相关系数可视化之corrplot包 https://zhuanlan.zhihu.com/p/28076189
- R语言相关关系可视化函数梳理 https://zhuanlan.zhihu.com/p/36925332
- 相关性分析了解一下 https://mp.weixin.qq.com/s/Nm9NEGG9gy-lEX34kyxFgQ?token=867568924&lang=zh_CN
- ggplot2最顶级的50种可视化图 http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html
- DT包用法 https://rstudio.github.io/DT/