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相關熱圖的資料請關註WalkonNet其它相關文章!

推薦閱讀: