介紹
本文介紹如何繪制相關(guān)圖中R.相關(guān)圖是相關(guān)矩陣的圖形基茵。突出顯示數(shù)據(jù)表中最相關(guān)的變量非常有用。在該圖中,相關(guān)系數(shù)根據(jù)該值著色倔韭。相關(guān)矩陣也可以根據(jù)變量之間的關(guān)聯(lián)程度進行重新排序。這里使用R corrplot軟件包瓢对。
請注意寿酌,這里也可以使用在線軟件來計算相關(guān)矩陣并繪制相關(guān)圖,而無需進行任何安裝硕蛹。
安裝R corrplot軟件包
要執(zhí)行本文中的R代碼醇疼,需要corrplot程序包。
install.packages("corrplot")
相關(guān)分析數(shù)據(jù)
該mtcars數(shù)據(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
計算相關(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ù)的簡化格式為:
corrplot(corr, method="circle")
參數(shù) | 描述 |
---|---|
更正 | 相關(guān)矩陣可視化。要可視化常規(guī)矩陣壶栋,請使用is.corr = FALSE。 |
方法 | 可視化方法:“圓圈”普监,“顏色”贵试,“數(shù)字”等 |
可視化方法
可以使用七種不同的可視化方法:“圓形”,“正方形”凯正,“橢圓”毙玻,“數(shù)字”,“陰影”廊散,“顏色”桑滩,“餅圖”( “circle”, “square”, “ellipse”, “number”, “shade”, “color”, “pie”)。
library(corrplot)
corrplot(M, method="circle")
corrplot(M, method="pie")
corrplot(M, method="color")
正相關(guān)以藍色顯示米者,負相關(guān)以紅色顯示韭畸。顏色強度和圓圈的大小與相關(guān)系數(shù)成正比。
顯示相關(guān)系數(shù):
corrplot(M, method="number")
相關(guān)圖布局的類型
布局分為三種:
- “ full”(默認):顯示完整的相關(guān)矩陣
- “ upper”:顯示相關(guān)矩陣的上三角
- “ lower”:顯示相關(guān)矩陣的下三角
corrplot(M, type="upper")
corrplot(M, type="lower")
重新排序相關(guān)矩陣
相關(guān)矩陣可以根據(jù)被重新排序相關(guān)系數(shù)妻顶。這對于確定矩陣中隱藏的結(jié)構(gòu)和圖案很重要酸员。在以下示例中蜒车,使用“ hclust”表示層次結(jié)構(gòu)的聚類順序。
# correlogram with hclust reordering
corrplot(M, type="upper", order="hclust")
# Using different color spectrum
col<- colorRampPalette(c("red", "white", "blue"))(20)
corrplot(M, type="upper", order="hclust", col=col)
# Change background color to lightblue
corrplot(M, type="upper", order="hclust", col=c("black", "white"),
bg="lightblue")
更改相關(guān)圖的顏色
如上節(jié)所示汇恤,可以自定義相關(guān)圖的顏色庞钢。RcolorBrewer調(diào)色板的顏色在以下R腳本中使用:
library(RColorBrewer)
corrplot(M, type="upper", order="hclust",
col=brewer.pal(n=8, name="RdBu"))
corrplot(M, type="upper", order="hclust",
col=brewer.pal(n=8, name="RdYlBu"))
corrplot(M, type="upper", order="hclust",
col=brewer.pal(n=8, name="PuOr"))
更改文本標簽的顏色和旋轉(zhuǎn)
tl.col(用于文本標簽顏色)和 tl.srt(用于文本標簽字符串旋轉(zhuǎn))用于更改文本的顏色和旋轉(zhuǎn)。
corrplot(M, type="upper", order="hclust", tl.col="black", tl.srt=45)
將相關(guān)圖與顯著性檢驗相結(jié)合
計算相關(guān)性的p值
為了計算p值矩陣,使用了一個自定義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)
# 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被認為是微不足道的遏暴。在這種情況下,相關(guān)系數(shù)值留為空白或添加叉號指黎。
自定義相關(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
)
結(jié)論
使用corrplot() R函數(shù)繪制相關(guān)矩陣的優(yōu)美圖袋励。
資訊
參考:corrplot介紹
This analysis was performed using R (ver. 3.1.0).
覺得有用的老鐵麻煩點個小愛心~??