如何设置 R corrplot 对角线数字标签?

How to set R corrplot diagonal numeric labels?

我想在对角线上获得标签,如图 3 所示,但带有 corrplot,如图 1-2 所示。 我正在研究数字对角线标签的 corrplot 手册 here。 我不知道有什么方法可以将数字标签放在 corrplot 对角线上,因为我设法伪造了所有可能的选择。 伪造的东西

部分事情很高兴了解更多但不限制我们

代码,还有 here 不同的例子,其中包括 K.J.J.K 的第一个答案的建议作为测试用例,但被证明是错误的任务

library("corrplot")

# http://www.sthda.com/english/wiki/visualize-correlation-matrix-using-correlogram
cor.mtest <- function(mat, diag.labels, ...) {
    mat <- as.matrix(mat)
    n <- ncol(mat)
    p.mat<- matrix(NA, n, n)
    diag(p.mat) <- 0
    for (i in 1:(n - 1)) {
        for (j in (i + 1):n) {
            tmp <- cor.test(mat[, i], mat[, j], ...)
            p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
        }
    }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat) <- diag.labels
  p.mat
}

ids <- c(seq(1,11))

M<-cor(mtcars)
p.mat <- cor.mtest(mtcars, diag.labels=ids)
corrplot(M, type="upper", order="hclust", diag=FALSE, # TODO tl.pos=c("d"),
         p.mat = p.mat, sig.level = 0.05)

图1 对角线上没有预期标签的输出, 图 2 伪造 K.J.J.K 的提议,其中对角线标签没有影响, 图 3 对角线标签示例 corrgram found

预期输出:如图 3 中对角线上的数字标签,但如图 (1-2) 中的其他装饰需要

伪造K.J.J.K的提议

获取代码 here,您将获得图 2 中对角线标签没有变化的输出。

OS:Debian 8.5
R: 3.3.1
开发者 Github 中的票证:#71

library("corrplot")

# http://rstudio-pubs-static.s3.amazonaws.com/6382_886fbab74fd5499ba455f11360f78de7.html
# plotcorr(R, col = colorRampPalette(c("#E08214", "white", "#8073AC"))(10), type = "lower")

# http://www.sthda.com/english/wiki/visualize-correlation-matrix-using-correlogram
# corrplot(M, type="upper", order="hclust", tl.col="black", tl.srt=45)

## Compute p-value of correlations
# mat : is a matrix of data
# ... : further arguments to pass to the native R cor.test function

M<-cor(mtcars)

# http://www.sthda.com/english/wiki/visualize-correlation-matrix-using-correlogram
cor.mtest <- function(mat, ...) {
  mat <- as.matrix(mat)
  n <- ncol(mat)
  p.mat<- matrix(NA, n, n)
  diag(p.mat) <- 0
  for (i in 1:(n - 1)) {
    for (j in (i + 1):n) {
      tmp <- cor.test(mat[, i], mat[, j], ...)
      p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
    }
  }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
  p.mat
}
# matrix of the p-value of the correlation
p.mat <- cor.mtest(mtcars)
head(p.mat[, 1:5])

corrplot(M, type="upper", order="hclust", 
  p.mat = p.mat, sig.level = 0.05)

# Leave blank on no significant coefficient
corrplot(M, type="upper", order="hclust", 
  p.mat = p.mat, sig.level = 0.01, insig = "blank")

col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
corrplot(M, method="color", col=col(200),  
  type="upper", order="hclust", 
  addCoef.col = "black", # Add coefficient of correlation
  tl.col="black", tl.srt=45, #Text label color and rotation
  # Combine with significance
  p.mat = p.mat, sig.level = 0.01, insig = "blank", 
  # hide correlation coefficient on the principal diagonal
  diag=FALSE 
)

ids <- c(seq(1,11))
M<-cor(mtcars)
colnames(M)<-ids
rownames(M)<-c("I","told","you","row","names","controls","the","diag","labels","kj","jk")

corrplot(M, type="upper",p.mat = p.mat, sig.level = 0.05)

我得到的输出: