一文讀懂R語(yǔ)言corrplot相關(guān)熱圖分析及美化?。。。ǜ綀D詳解,建議收藏)

gzh原文點(diǎn)我,歡迎同時(shí)關(guān)注

介紹

R corrplot包 提供了一個(gè)在相關(guān)矩陣上的可視化探索工具,該工具支持自動(dòng)變量重新排序,以幫助檢測(cè)變量之間的隱藏模式。

corrplot 非常易于使用,并在可視化方法、圖形布局、顏色、圖例、文本標(biāo)簽等方面提供了豐富的繪圖選項(xiàng)。它還提供 p 值和置信區(qū)間,以幫助用戶確定相關(guān)性的統(tǒng)計(jì)顯著性。

corrplot()有大約50個(gè)參數(shù),但最常見(jiàn)的參數(shù)只有幾個(gè)。在大多數(shù)場(chǎng)景中,我們可以得到一個(gè)只有一行代碼的相關(guān)矩陣圖。

1.加載包

library(corrplot)

2.加載數(shù)據(jù)

mtcars

3.繪圖

corrplot(M, method = 'number')
圖片
#order排序方法original(默認(rèn)),特征向量角度排序AOE,第一個(gè)主成分順序FPC,分層聚類排序hclust,按照字母排序alphabet
corrplot(M, method = 'color', order = 'hclust')
圖片
#形狀默認(rèn)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僅僅顯示下部分相關(guān)性,除此之外還有參數(shù)full,upper
corrplot(M, method = 'square', order = 'FPC', type = 'lower', diag = FALSE)

圖片
corrplot(M, method = 'ellipse', order = 'FPC', type = 'upper', diag = FALSE)
圖片
#數(shù)字和圖混合
corrplot.mixed(M, order = 'AOE')
圖片
#混合上部餅圖,下部陰影
corrplot.mixed(M, lower = 'shade', upper = 'pie', order = 'hclust')
圖片
#分層聚類,標(biāo)出2個(gè)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.個(gè)性化設(shè)置聚類方法


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.個(gè)性化添加矩陣

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.顏色設(shè)置

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.*參數(shù)常用于顏色圖例:cl.pos顏色標(biāo)簽的位置('r'type='upper''full''b'type='lower''n'),cl.ratio顏色圖例的寬度建議0.1~0.2
#tl.*參數(shù)常用于文本圖例:tl.pos用于文本標(biāo)簽的位置,tl.cex文本大小,tl.srt文本的旋轉(zhuǎn)
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')
圖片
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容