R語(yǔ)言繪制corrplot相關(guān)熱圖分析美化示例及詳細(xì)圖解
介紹
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')

以上就是R語(yǔ)言corrplot相關(guān)熱圖分析美化示例及詳細(xì)圖解的詳細(xì)內(nèi)容,更多關(guān)于R語(yǔ)言corrplot相關(guān)熱圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
R語(yǔ)言導(dǎo)入CSV數(shù)據(jù)的簡(jiǎn)單方法
這篇文章主要介紹了R語(yǔ)言導(dǎo)入CSV數(shù)據(jù)的簡(jiǎn)單方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
R語(yǔ)言數(shù)據(jù)可視化繪圖bar chart條形圖實(shí)現(xiàn)示例
這篇文章主要為大家介紹了R語(yǔ)言數(shù)據(jù)可視化繪圖bar chart條形圖的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
R語(yǔ)言數(shù)據(jù)類型與相應(yīng)運(yùn)算的實(shí)現(xiàn)
本文主要介紹了R語(yǔ)言數(shù)據(jù)類型與相應(yīng)運(yùn)算的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
R語(yǔ)言學(xué)習(xí)RcppEigen進(jìn)行矩陣運(yùn)算
這篇文章主要為大家介紹了R語(yǔ)言學(xué)習(xí)如何利用RcppEigen進(jìn)行矩陣運(yùn)算的實(shí)現(xiàn)方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
基于R語(yǔ)言賦值符號(hào)的區(qū)別說(shuō)明
這篇文章主要介紹了基于R語(yǔ)言賦值符號(hào)的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
R語(yǔ)言時(shí)間序列TAR閾值自回歸模型示例詳解
這篇文章主要介紹了R語(yǔ)言時(shí)間序列TAR閾值自回歸模型,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
R語(yǔ)言 實(shí)現(xiàn)將1對(duì)多數(shù)據(jù)與1對(duì)1數(shù)據(jù)互換
這篇文章主要介紹了R語(yǔ)言 實(shí)現(xiàn)將1對(duì)多數(shù)據(jù)與1對(duì)1數(shù)據(jù)互換的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
R語(yǔ)言在散點(diǎn)圖中添加lm線性回歸公式的問(wèn)題
這篇文章主要介紹了R語(yǔ)言在散點(diǎn)圖中添加lm線性回歸公式的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09

