相关图

今天学习文献(Pagliarini2016),看到这样的相关系数图:


Pagliarini2016

以往通常用表的形式:


Sun2003.jpg

或者用矩阵图的形式:


Dong2017

像Pagliarini2016中的热图头一次见于文献(有点炫,适合PPT用)。那怎么实现的呢?corrplot可以做这个事:

## 构建相关矩阵
M<-cor(mtcars)
## 热图
corrplot(M, method="circle")

因为上对角和下对角是一样的,也可以只有一半:

corrplot(M, type="upper")

但是这些热图有个问题是,不知道显著性。corrplot也可以实现:

  1. 获得显著性矩阵
# 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])
  1. 个性化相关图
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 
         )

这就比较理想了。

参考资料:Visualize correlation matrix using correlogram

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容