R語言繪制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ù),但最常見的參數(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語言corrplot相關(guān)熱圖分析美化示例及詳細(xì)圖解的詳細(xì)內(nèi)容,更多關(guān)于R語言corrplot相關(guān)熱圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
R語言基于Keras的MLP神經(jīng)網(wǎng)絡(luò)及環(huán)境搭建
這篇文章主要介紹了R語言基于Keras的MLP神經(jīng)網(wǎng)絡(luò),我并沒有使用python去對(duì)比結(jié)果,但NSS的文章中有做對(duì)比,數(shù)據(jù)顯示R與Python相比在各方面的差別都不大,具體內(nèi)容介紹跟隨小編一起看看吧2022-01-01R語言中R-squared與Adjust R-squared參數(shù)的解釋
這篇文章主要給大家介紹了關(guān)于R語言中R-squared與Adjust R-squared兩個(gè)參數(shù)的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03R語言數(shù)據(jù)類型知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于R語言數(shù)據(jù)類型知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03