corrplot shows insignificant correlation coefficients even when insig = "blank" is set
Asked Answered
I

2

5

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 https://mcmap.net/q/513881/-how-to-change-font-size-of-the-correlation-coefficient-in-corrplot/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:

enter image description here

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)

Perhaps this is a bug of corrplot package?

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

Ingrown answered 26/10, 2014 at 15:30 Comment(1)
You could start by putting a library call to load the package with the function.Coquelicot
K
11

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)

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)

enter image description here


EDIT re @Masi's comments

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))
Karen answered 26/10, 2014 at 16:45 Comment(2)
Thanks, this is a great answer! It is very stupid that in case of order="AOE" the order must be calculated outside of the corrplot function, as you mention! This is a flaw of the corrplot function. The addCoef.col should take a matrix that would be of the same order & format as the correlation matrix.Ingrown
I found a better method! Just do cl.lim=c(-100,100), it automatically scales the matrix values to [-100, 100], so you do not need to multiply anything and not even change the colourbar, just use the default.Compatible
A
0

If you're not too picky, you could also leave the background set to white and make your addCoef.col = "white" instead of "grey" as you have it originally. That would eliminate the need for the ordering and ifelse statements.

Ajar answered 18/4, 2016 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.