使用corrplot包可視化相關(guān)性矩陣

介紹

本文介紹如何繪制相關(guān)圖中R.相關(guān)圖相關(guān)矩陣的圖形。突出顯示數(shù)據(jù)表中最相關(guān)的變量非常有用。在該圖中,相關(guān)系數(shù)根據(jù)該值著色。相關(guān)矩陣也可以根據(jù)變量之間的關(guān)聯(lián)程度進(jìn)行重新排序。這里使用R corrplot軟件包。

請(qǐng)注意,這里也可以使用在線軟件來計(jì)算相關(guān)矩陣并繪制相關(guān)圖,而無需進(jìn)行任何安裝。

安裝R corrplot軟件包

要執(zhí)行本文中的R代碼,需要corrplot程序包。

install.packages("corrplot")

相關(guān)分析數(shù)據(jù)

mtcars數(shù)據(jù)被用于計(jì)算相關(guān)矩陣。

head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

計(jì)算相關(guān)矩陣

M<-cor(mtcars)
head(round(M,2))
       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
mpg   1.00 -0.85 -0.85 -0.78  0.68 -0.87  0.42  0.66  0.60  0.48 -0.55
cyl  -0.85  1.00  0.90  0.83 -0.70  0.78 -0.59 -0.81 -0.52 -0.49  0.53
disp -0.85  0.90  1.00  0.79 -0.71  0.89 -0.43 -0.71 -0.59 -0.56  0.39
hp   -0.78  0.83  0.79  1.00 -0.45  0.66 -0.71 -0.72 -0.24 -0.13  0.75
drat  0.68 -0.70 -0.71 -0.45  1.00 -0.71  0.09  0.44  0.71  0.70 -0.09
wt   -0.87  0.78  0.89  0.66 -0.71  1.00 -0.17 -0.55 -0.69 -0.58  0.43

相關(guān)圖:可視化相關(guān)矩陣

R corrplot函數(shù)用于繪制相關(guān)矩陣。

該函數(shù)的簡(jiǎn)化格式為:

corrplot(corr, method="circle")
參數(shù) 描述
更正 相關(guān)矩陣可視化。要可視化常規(guī)矩陣,請(qǐng)使用is.corr = FALSE。
方法 可視化方法:“圓圈”,“顏色”,“數(shù)字”等

可視化方法

可以使用七種不同的可視化方法:“圓形”,“正方形”,“橢圓”,“數(shù)字”,“陰影”,“顏色”,“餅圖”( “circle”, “square”, “ellipse”, “number”, “shade”, “color”, “pie”)。

library(corrplot)
corrplot(M, method="circle")
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖
corrplot(M, method="pie")
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖
corrplot(M, method="color")
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖

正相關(guān)以藍(lán)色顯示,負(fù)相關(guān)以紅色顯示。顏色強(qiáng)度和圓圈的大小與相關(guān)系數(shù)成正比。

顯示相關(guān)系數(shù)

corrplot(M, method="number")
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖

相關(guān)圖布局的類型

布局分為三種:

  • “ full”(默認(rèn)):顯示完整的相關(guān)矩陣
  • “ upper”:顯示相關(guān)矩陣的上三角
  • “ lower”:顯示相關(guān)矩陣的下三角
corrplot(M, type="upper")
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖
corrplot(M, type="lower")
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖

重新排序相關(guān)矩陣

相關(guān)矩陣可以根據(jù)被重新排序相關(guān)系數(shù)。這對(duì)于確定矩陣中隱藏的結(jié)構(gòu)和圖案很重要。在以下示例中,使用“ hclust”表示層次結(jié)構(gòu)的聚類順序。

# correlogram with hclust reordering
corrplot(M, type="upper", order="hclust")
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖
# Using different color spectrum
col<- colorRampPalette(c("red", "white", "blue"))(20)
corrplot(M, type="upper", order="hclust", col=col)
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖
# Change background color to lightblue
corrplot(M, type="upper", order="hclust", col=c("black", "white"),
         bg="lightblue")
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖

更改相關(guān)圖的顏色

如上節(jié)所示,可以自定義相關(guān)圖的顏色。RcolorBrewer調(diào)色板的顏色在以下R腳本中使用:

library(RColorBrewer)
corrplot(M, type="upper", order="hclust", 
         col=brewer.pal(n=8, name="RdBu"))
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖
corrplot(M, type="upper", order="hclust",
         col=brewer.pal(n=8, name="RdYlBu"))
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖
corrplot(M, type="upper", order="hclust",
         col=brewer.pal(n=8, name="PuOr"))
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖

更改文本標(biāo)簽的顏色和旋轉(zhuǎn)

tl.col(用于文本標(biāo)簽顏色)和 tl.srt(用于文本標(biāo)簽字符串旋轉(zhuǎn))用于更改文本的顏色和旋轉(zhuǎn)。

corrplot(M, type="upper", order="hclust", tl.col="black", tl.srt=45)
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖

將相關(guān)圖與顯著性檢驗(yàn)相結(jié)合

計(jì)算相關(guān)性的p值

為了計(jì)算p值矩陣,使用了一個(gè)自定義R函數(shù):

# mat : is a matrix of data
# ... : further arguments to pass to the native R cor.test function
cor.mtest <- function(mat, ...) {
    mat <- as.matrix(mat)
    n <- ncol(mat)
    p.mat<- matrix(NA, n, n)
    diag(p.mat) <- 0
    for (i in 1:(n - 1)) {
        for (j in (i + 1):n) {
            tmp <- cor.test(mat[, i], mat[, j], ...)
            p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
        }
    }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
  p.mat
}
# matrix of the p-value of the correlation
p.mat <- cor.mtest(mtcars)
head(p.mat[, 1:5])
           mpg       cyl      disp        hp      drat
mpg  0.000e+00 6.113e-10 9.380e-10 1.788e-07 1.776e-05
cyl  6.113e-10 0.000e+00 1.803e-12 3.478e-09 8.245e-06
disp 9.380e-10 1.803e-12 0.000e+00 7.143e-08 5.282e-06
hp   1.788e-07 3.478e-09 7.143e-08 0.000e+00 9.989e-03
drat 1.776e-05 8.245e-06 5.282e-06 9.989e-03 0.000e+00
wt   1.294e-10 1.218e-07 1.222e-11 4.146e-05 4.784e-06

向相關(guān)圖添加顯著性水平

# Specialized the insignificant value according to the significant level
corrplot(M, type="upper", order="hclust", 
         p.mat = p.mat, sig.level = 0.01)
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖
# Leave blank on no significant coefficient
corrplot(M, type="upper", order="hclust", 
         p.mat = p.mat, sig.level = 0.01, insig = "blank")

另外,在上述圖中,相關(guān)性p-值> 0.01被認(rèn)為是微不足道的。在這種情況下,相關(guān)系數(shù)值留為空白或添加叉號(hào)。

自定義相關(guān)圖

col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
corrplot(M, method="color", col=col(200),  
         type="upper", order="hclust", 
         addCoef.col = "black", # Add coefficient of correlation
         tl.col="black", tl.srt=45, #Text label color and rotation
         # Combine with significance
         p.mat = p.mat, sig.level = 0.01, insig = "blank", 
         # hide correlation coefficient on the principal diagonal
         diag=FALSE 
         )
相關(guān)矩陣,R中的相關(guān)圖,相關(guān)圖

結(jié)論

使用corrplot() R函數(shù)繪制相關(guān)矩陣的優(yōu)美。

資訊

參考corrplot介紹

This analysis was performed using R (ver. 3.1.0).


覺得有用的老鐵麻煩點(diǎn)個(gè)小愛心~??

最后編輯于
?著作權(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)容