Corrplot shows insignificant correlation coefficients even if insig = "blank" is set
I like to use a correlation plot using a function corrplot
with the correlation coefficients printed in the cells (using addCoef.col
and addCoefasPercent = TRUE
). I would also like to remove minor correlations from the graph (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 being printed! Cm:
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 the coefficients are being printed for nonessential cells as well:
To see which cells are irrelevant, 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 in the corrplot package?
How can I get rid of printing coefficient in minor cells?
source to share
You need to do a little work for this. You need to define the color vector manually for p-values, which is passed toaddCoef.col
If you ordered in alphabetical order, that's straightforward
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 since you want to order by eigenvalues, you need to compute the order outside the function 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)
EDIT re @Masi comments
To update the limits on the color bar, 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))
source to share