这篇文章主要介绍“R语言corrplot相关热图美化实例分析”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“R语言corrplot相关热图美化实例分析”文章能帮助大家解决问题。
介绍
R corrplot包 提供了一个在相关矩阵上的可视化探索工具,该工具支持自动变量重新排序,以帮助检测变量之间的隐藏模式。
corrplot 非常易于使用,并在可视化方法、图形布局、颜色、图例、文本标签等方面提供了丰富的绘图选项。它还提供 p 值和置信区间,以帮助用户确定相关性的统计显著性。
corrplot()有大约50个参数,但最常见的参数只有几个。在大多数场景中,我们可以得到一个只有一行代码的相关矩阵图。
1.加载包
library(corrplot)
2.加载数据
mtcars
3.绘图
corrplot(M, method = 'number')
#order排序方法original(默认),特征向量角度排序AOE,第一个主成分顺序FPC,分层聚类排序hclust,按照字母排序alphabetcorrplot(M, method = 'color', order = 'hclust')
#形状默认circle,除此之外还有square,ellipse,number,pie,shade,colorcorrplot(M,method="circle")
corrplot(M,method="square")
corrplot(M,method="ellipse")
corrplot(M,method="pie")
#diag = FALSE,不显示中间为1的格子corrplot(M,method="square",diag = FALSE)
#type仅仅显示下部分相关性,除此之外还有参数full,uppercorrplot(M, method = 'square', order = 'FPC', type = 'lower', diag = FALSE)
corrplot(M, method = 'ellipse', order = 'FPC', type = 'upper', diag = FALSE)
#数字和图混合corrplot.mixed(M, order = 'AOE')
#混合上部饼图,下部阴影corrplot.mixed(M, lower = 'shade', upper = 'pie', order = 'hclust')
#分层聚类,标出2个clustercorrplot(M, order = 'hclust', addrect = 2)
#定义圈出的cluster,以及圈出线的颜色和线条corrplot(M, method = 'square', diag = FALSE, order = 'hclust', addrect = 3, rect.col = 'blue', rect.lwd = 3, tl.pos = 'd')
4.个性化设置聚类方法
install.packages("seriation")library(seriation)list_seriation_methods('matrix')list_seriation_methods('dist')data(Zoo)Z = cor(Zoo[, -c(15, 17)])dist2order = function(corr, method, ...) { d_corr = as.dist(1 - corr) s = seriate(d_corr, method = method, ...) i = get_order(s) return(i)}# Fast Optimal Leaf Ordering for Hierarchical Clusteringi = dist2order(Z, 'OLO')corrplot(Z[i, i], cl.pos = 'n')
# Quadratic Assignment Problemi = dist2order(Z, 'QAP_2SUM')corrplot(Z[i, i], cl.pos = 'n')
# Multidimensional Scalingi = dist2order(Z, 'MDS_nonmetric')corrplot(Z[i, i], cl.pos = 'n')
5.个性化添加矩阵
library(magrittr)#方法1i = dist2order(Z, 'R2E')corrplot(Z[i, i], cl.pos = 'n') %>% corrRect(c(1, 9, 15))
#方法2corrplot(Z, order = 'AOE') %>% corrRect(name = c('tail', 'airborne', 'venomous', 'predator'))
#方法3直接指定r = rbind(c('eggs', 'catsize', 'airborne', 'milk'), c('catsize', 'eggs', 'milk', 'airborne'))corrplot(Z, order = 'hclust') %>% corrRect(namesMat = r)
6.颜色设置
COL1(sequential = c("Oranges", "Purples", "Reds", "Blues", "Greens", "Greys", "OrRd", "YlOrRd", "YlOrBr", "YlGn"), n = 200)COL2(diverging = c("RdBu", "BrBG", "PiYG", "PRGn", "PuOr", "RdYlBu"), n = 200)#cl.*参数常用于颜色图例:cl.pos颜色标签的位置('r'type='upper''full''b'type='lower''n'),cl.ratio颜色图例的宽度建议0.1~0.2#tl.*参数常用于文本图例:tl.pos用于文本标签的位置,tl.cex文本大小,tl.srt文本的旋转
corrplot(M, order = 'AOE', col = COL2('RdBu', 10))
corrplot(M, order = 'AOE', addCoef.col = 'black', tl.pos = 'd', cl.pos = 'r', col = COL2('PiYG'))
corrplot(M, method = 'square', order = 'AOE', addCoef.col = 'black', tl.pos = 'd', cl.pos = 'r', col = COL2('BrBG'))
corrplot(M, order = 'AOE', cl.pos = 'b', tl.pos = 'd',col = COL2('PRGn'), diag = FALSE)
corrplot(M, type = 'lower', order = 'hclust', tl.col = 'black', cl.ratio = 0.2, tl.srt = 45, col = COL2('PuOr', 10))
corrplot(M, order = 'AOE', cl.pos = 'n', tl.pos = 'n', col = c('white', 'black'), bg = 'gold2')
关于“R语言corrplot相关热图美化实例分析”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网行业资讯频道,小编每天都会为大家更新不同的知识点。