Reporting significance level in corrplot()

2019-01-26 22:21发布

问题:

I'm currently using corrplot() from the corrplot package in R and I've stumbled across two problems. For simplicity, I'll use the same notation as the help/introduction page for corrplot.

  1. I'd like to inscribe either my p-value or how significant the test was (or both!) in all cells, not just the insignificant ones.

  2. I'd like these inscriptions only in the upper triangular.

To address 2) first, I've been able to use this, but if feels kind of hacky:

corrplot(M, type="upper", p.mat = res1[[1]], insig="p-value", tl.pos="n")
corrplot(M, type="lower", add=T, tl.pos="d", cl.pos="n"))

However I haven't been able to figure out number 1. Any suggestions would be helpful!

回答1:

The quick way is to add sig.level=0 to the first plot, so all p-values are shown (actually, due to numerical precision some p-values will be exactly zero, so you may need to set it to sig.level=-0.1, for example)

require(corrplot)

# Data
M <- mtcars[3:7]
pval <- psych::corr.test(M, adjust="none")$p

# Corrplot
corrplot(cor(M), type="upper", p.mat=pval, insig="p-value", 
                                               tl.pos="n", sig.level=0)
corrplot(cor(M), type="lower", add=T, tl.pos="d", cl.pos="n")

This gives

However, if you want to add more detail to the p values it is probably easier to post-format the plot and add them using a text call

# Plot
corrplot(cor(M), type="upper", tl.pos="n")

# Get positions & plot formatted p-values
pos <- expand.grid(1:ncol(pval), ncol(pval):1)
text(pos, p_format(pval))

# lower tri
corrplot(cor(M), type="lower", add=T, tl.pos="d", cl.pos="n")

To give

Format function

p_format <- function(x, ndp=3)
{
  out <- format(round(as.numeric(x),ndp),ns=ndp,scientific=F,just="none")
  ifelse(out=="0.000","<0.0001", out)
}

My view (fwiw) is that this is adding too much info to the plot



标签: r r-corrplot