本文介绍了甚至当insig ="blank"时,corrplot仍显示无关紧要的系数.被设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢使用使用corrplot函数的相关图,并在单元格中打印相关系数(使用addCoef.coladdCoefasPercent = TRUE).我还想从图中删除无关紧要的关联(使用insig = "blank").问题在于,这仅适用于背景颜色,而不适用于系数本身,因此系数本身仍会打印!参见:

I like to use correlation plot using corrplot function with correlation coefficients printed in the cells (using addCoef.col and addCoefasPercent = TRUE). I also like to remove the insignificant correlations from the plot (using insig = "blank"). The problem is that this only works for the background color, but not for the coefficient itself, so the coefficient itself is still printed! See:

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)

现在您可以看到,对于无关紧要的单元格,系数也被打印出来了:

Now you can see that the coefficients are printed also for the insignificant cells:

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

Just to see which cells are insignificant, you can use this command:

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程序包的错误吗?

Perhaps this is a bug of corrplot package?

如何摆脱在无关紧要的单元格中打印系数?

推荐答案

为此您需要做一些工作.您需要为p值手动定义颜色向量,该向量将传递给addCoef.col

You have to do a little work for this. You need to define a vector of colours manually for the p-values, that is passed to addCoef.col

如果您是按字母顺序订购,那就很简单

If you were ordering alphabetically, it is straight forward

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函数外部的排序

But as you want to order by the eigenvalues you need to calculate the ordering outside of the corrplot function

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)

编辑是@Masi的评论

要更新彩条上的限制,请使用cl.lim

To update the limits on the colourbar set the limits with 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))

这篇关于甚至当insig ="blank"时,corrplot仍显示无关紧要的系数.被设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:00