首页 文章

即使设置insig =“blank”,corrplot也显示无关紧要的相关系数

提问于
浏览
3

我喜欢使用 corrplot 函数的相关图,其中相关系数打印在单元格中(使用 addCoef.coladdCoefasPercent = TRUE ) . 我也想从图中删除无关紧要的相关性(使用 insig = "blank" ) . 问题是这只适用于背景颜色,但不适用于系数本身,所以系数本身仍然打印!看到:

set.seed(123)
par(cex=0.8) # trick for cor. coef font size, see http://stackoverflow.com/q/26574054/684229
col1 <-rainbow(100, s = 1, v = 1, start = 0, end = 0.9, alpha = 1)
test <- matrix(data=rnorm(400),nrow=20,ncol=20)


cor.mtest <- function(mat, conf.level = 0.95){
    mat <- as.matrix(mat)
    n <- ncol(mat)
    p.mat <- lowCI.mat <- uppCI.mat <- matrix(NA, n, n)
    diag(p.mat) <- 0
    diag(lowCI.mat) <- diag(uppCI.mat) <- 1
    for(i in 1:(n-1)){
        for(j in (i+1):n){
            tmp <- cor.test(mat[,i], mat[,j], conf.level = conf.level)
            p.mat[i,j] <- p.mat[j,i] <- tmp$p.value
            lowCI.mat[i,j] <- lowCI.mat[j,i] <- tmp$conf.int[1]
            uppCI.mat[i,j] <- uppCI.mat[j,i] <- tmp$conf.int[2]
        }
    }
    return(list(p.mat, lowCI.mat, uppCI.mat))
}

cor1 <- cor.mtest(test, 0.95)

corrplot(cor(test), p.mat = cor1[[1]], insig = "blank", method = "color", addCoef.col="grey", 
    order = "AOE", tl.cex = 1/par("cex"),
    cl.cex = 1/par("cex"), addCoefasPercent = TRUE)

现在你可以看到,对于无关紧要的细胞也会打印系数:

enter image description here

只是为了查看哪些单元格无关紧要,您可以使用以下命令:

corrplot(cor(test), p.mat = cor1[[1]], insig = "pch", method = "color", addCoef.col="grey", 
    order = "AOE", tl.cex = 1/par("cex"),
    cl.cex = 1/par("cex"), addCoefasPercent = TRUE)

也许这是corrplot包的错误?

How can I get rid of printing the coefficient in insignificant cells?

2 回答

  • 4

    你必须为此做一点工作 . 您需要手动为p值定义颜色矢量,传递给 addCoef.col

    如果按字母顺序排序,则可以直接进行

    mycol <- ifelse(c(cor1[[1]] < 0.05), "black", "white")
    
    corrplot(cor(test), p.mat = cor1[[1]] , insig = "blank", method = "color", 
             addCoef.col=mycol ,
             order = "original", tl.cex = 1/par("cex"), 
             cl.cex = 1/par("cex"), addCoefasPercent = TRUE)
    

    但是,如果您想按特征值排序,则需要计算 corrplot 函数之外的排序

    ord <-   corrMatOrder(cor(test), order="AOE")
    M <- cor(test)[ord, ord]
    
    pval <- psych::corr.test(data.frame(test), adjust="none")$p[ord, ord]
    mycol <- ifelse(c(pval < 0.05), "black", "white")
    
    
    corrplot(M, p.mat = pval , insig = "blank", method = "color", addCoef.col=mycol ,
             order = "original", tl.cex = 1/par("cex"), 
             cl.cex = 1/par("cex"), addCoefasPercent = TRUE)
    

    enter image description here


    EDIT 回复@Masi的评论

    要更新颜色栏上的限制,请使用 cl.lim 设置限制

    corrplot(cor(test), p.mat = cor1[[1]] , insig = "blank", method = "color", 
           addCoef.col=mycol , addCoefasPercent=TRUE, 
           order = "original", cl.lim = c(-100, 100))
    
  • 0

    如果你不太挑剔,你也可以将背景设置为白色并使你的 addCoef.col = "white" 而不是 "grey" ,就像你原来的那样 . 这将消除对order和ifelse语句的需要 .

相关问题