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

时间:2016-11-08 19:32:04

标签: r correlation diagonal r-corrplot

我希望在对角线上获得标签,如图3所示,但是corrplot如图1-2所示。 我正在研究corrplot手册here以获取数字对角线标签。 我不知道任何允许在corrplot对角线上放置数字标签的方法,因为我设法伪造了所有潜在的选择。 伪造的东西

  • 数字对角线标签无法通过函数cor.mtest

    中的以下行进行设置
    colnames(p.mat) <- rownames(p.mat) <- colnames(mat) <- diag.labels
    
  • colorlegend显然不是正确的选择

    corrplot(...)
    colorlegend(colbar = grey(1:100 / 100), labels=ids, addlabels = TRUE)
    

部分事情很高兴知道但不限制我们

  • diag=FALSE, tl.pos="d"适用于单个单元格。如何为N个细胞提供tl.pos="d"? - - tl.pos=c("d")会导致错误。 - - 数字对角标签是否需要tl.pos

代码,但here代表不同的例子,其中 K.J.J.K 的第一个答案提案作为测试用例,但对于任务显示为false

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

enumerated()对角线上的标签示例

here enter image description here enter image description here

预期输出:对角线上的数字标签,如图3所示,但装饰另有所需,如图(1-2)所示

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

获取代码enter image description here,得到图2中对角线标签没有变化的输出。

操作系统:Debian 8.5
R:3.3.1
开发人员的Github中的票证:here

1 个答案:

答案 0 :(得分:2)

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)

我得到的输出:Output