首頁 > 軟體

R語言繪製corrplot相關熱圖分析美化範例及詳細圖解

2022-06-24 18:02:22

介紹

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相關熱圖的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com