如何在 corrplot() 中以数字方式可视化置信区间?

How can confidence intervals be numerically visualized in corrplot()?

corrplot() 中,是否可以将系数下方的置信区间可视化?

library(corrplot)
M <- cor(mtcars)


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))
}

res1 <- cor.mtest(mtcars,0.95)
res2 <- cor.mtest(mtcars,0.99)

我想在下面的图中添加 low=res1[[2]]upp=res1[[3]] 置信区间作为相关系数下方的数字。

corrplot(M, method="number")

corrplot是一篇漂亮的文字text()table。所以我们可以尝试在上面添加额外的文本。

继续你的例子:

corrplot(cor(mtcars), method="number")

我们形成置信区间标签:

conf <- paste0("[", format(res1[[2]], digits=1), ":", format(res1[[3]], digits=1), "]")

并将它们作为文本添加到现有 corrplot:

xs <- row(res1[[1]])
ys <- (ncol(res1[[1]])+1) - col(res1[[1]])
text(xs, ys, conf, pos=1, cex=0.5)

注意:似乎 y=1 从顶部开始,所以我们需要将其反转(这就是为什么 ys 表达式比 xs.

更复杂的原因

结果如下: