R语言

关注公众号 jb51net

关闭
首页 > 软件编程 > R语言 > R语言corrplot相关热图

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,按照字母排序alphabet
corrplot(M, method = 'color', order = 'hclust')

#形状默认circle,除此之外还有square,ellipse,number,pie,shade,color
corrplot(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,upper
corrplot(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个cluster
corrplot(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 Clustering
i = dist2order(Z, 'OLO')
corrplot(Z[i, i], cl.pos = 'n')

# Quadratic Assignment Problem
i = dist2order(Z, 'QAP_2SUM')
corrplot(Z[i, i], cl.pos = 'n')

# Multidimensional Scaling
i = dist2order(Z, 'MDS_nonmetric')
corrplot(Z[i, i], cl.pos = 'n')

5.个性化添加矩阵

library(magrittr)
#方法1
i = dist2order(Z, 'R2E')
corrplot(Z[i, i], cl.pos = 'n') %>% corrRect(c(1, 9, 15))

#方法2
corrplot(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相关热图分析美化示例及详细图解的详细内容,更多关于R语言corrplot相关热图的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文