今天學(xué)習(xí)文獻(Pagliarini2016),看到這樣的相關(guān)系數(shù)圖:
以往通常用表的形式:
或者用矩陣圖的形式:
像Pagliarini2016中的熱圖頭一次見于文獻(有點炫霜定,適合PPT用)摊唇。那怎么實現(xiàn)的呢痴脾?corrplot
可以做這個事:
## 構(gòu)建相關(guān)矩陣
M<-cor(mtcars)
## 熱圖
corrplot(M, method="circle")
因為上對角和下對角是一樣的驼仪,也可以只有一半:
corrplot(M, type="upper")
但是這些熱圖有個問題是觅闽,不知道顯著性艘儒。corrplot
也可以實現(xiàn):
- 獲得顯著性矩陣
# 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])
- 個性化相關(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
)
這就比較理想了聋伦。