如何对 R corrplot 中的某些单元格边界进行着色?

How to colourise some cell borders in R corrplot?

我想通过使它们的边界与其他任何东西明显不同来让一些细胞受到关注。

参数 rect.col 用于为所有边框着色,但我只想为单元格 (3,3) 和 (7,7) 的边框着色,例如,通过任何光晕颜色等 heat.colors(100)rainbow(12).

代码:

library("corrplot")
library("psych")

ids <- seq(1,11) 

M.cor <- cor(mtcars)
colnames(M.cor) <- ids
rownames(M.cor) <- ids

p.mat <- psych::corr.test(M.cor, adjust = "none", ci = F)
p.mat <- p.mat[["r"]]

corrplot(M.cor, 
  method = "color", 
  type = "upper", 
  tl.col = 'black', 
  diag = TRUE, 
  p.mat = p.mat, 
  sig.level = 0.0000005
)

图1 没有单元格边界的顶部代码输出, 图 2 手动将所有坐标转换为上三角后的输出,但在 (10,1),
处存在伪影 图 3 window 大小固定

的输出

输入:位置按 ID (3,3)(7,7)
预期输出:两个单元格,其中边界标记在上三角形

伪代码

# ids must be id.pairs  
# or just a list of two lists
createBorders <- function(id.pairs) {

  labbly(id.pairs,function(z){
    x <- z$V1
    y <- z$V2
    rect(x+0.5, y+0.5, x+1.5, y+1.5) # user20650

  })
}

corrplot(...)
# TODO Which datastructure to use there in the function as the paired list of ids? 
createBorders(ids.pairs)

正在测试 user20650 的提案

rect(2+0.5, 9+0.5, 3+0.5, 10+0.5, border="white", lwd=2)

输出如图2。 如果有这个功能就好了。 假设您有一个 ID 列表。

我认为位置有问题,因为 (2,3),(9,10) 指向 (2,3),(2,3) 中的点。

在聊天中迭代 user20650 的提案

library("corrplot")
library("psych")

ids <- seq(1,11)

M.cor <- cor(mtcars)
colnames(M.cor) <- ids
rownames(M.cor) <- ids

p.mat <- psych::corr.test(M.cor, adjust = "none", ci = F)
p.mat <- p.mat[["r"]]

# Chat of  user20650
cb <- function(corrPlot, ..., rectArgs = list() ){
        lst <- list(...)
                n <- ncol(corrPlot)
                nms <- colnames(corrPlot)
                colnames(corrPlot) <- if(is.null(nms)) 1:ncol(corrPlot) else nms

                xleft <- match(lst$x, colnames(corrPlot)) - 0.5
                ybottom <- n - match(lst$y, colnames(corrPlot)) + 0.5

                lst <- list(xleft=xleft, ybottom=ybottom, xright=xleft+1, ytop=ybottom+1)
                do.call(rect, c(lst, rectArgs))
}
plt <- corrplot(M.cor,
                method = "color",
                type = "upper",
                tl.col = 'black',
                diag = TRUE,
                p.mat = p.mat,
                sig.level = 0.0000005
               )
cb(plt, x=c(1, 3, 5), y=c(10, 7, 4), rectArgs=list(border="white", lwd=3))

图 3 中只有一个单元格边框标记的输出。

预期输出:标记为

的三个单元格边框

图2方法中的限制

你必须首先将所有坐标都处理到上三角。 因此,您现在只能调用以下代码,其中输出在图 2 中的 (10,1) 处有一个工件

cb(plt, x=c(10, 7, 5), y=c(1, 3, 4), rectArgs=list(border="white", lwd=3))

预期输出:在 (10,1) 处没有伪影

伪影的原因可能是白色背景,但如果边框颜色为 red,也会出现伪影,因此很可能不是原因。 解决方案 - 修复 window 大小及其在图 3

中的输出
pdf("Rplots.pdf", height=10, width=10)
plt <- corrplot(M.cor,
                method = "color",
                type = "upper",
                tl.col = 'black',
                diag = TRUE,
                p.mat = p.mat,
                sig.level = 0.0000005
               )
cb(plt, x=c(10, 7, 5), y=c(1, 3, 4), rectArgs=list(border="red", lwd=3))
dev.off()

R: 3.3.1
OS:Debian 8.5
文档错误图:here

我的建议仍然是伪代码 mark.ids。我发现最好将 pltmark.ids 作为 corrplotCellBorders 的选项,这会创建带边框的想要的单元格

的 corrplot
mark.ids <- {x <- c(1), y <- c(2)} # TODO pseudocode
corrplotCellBorders(plt, mark.ids)
cb(plt, x, y, rectArgs=list(border="red", lwd=3))

# Chat of  user20650
# createBorders.r, test.createBorders. 
cb <- function(corrPlot, ..., rectArgs = list() ){ 
# ... pass named vector of x and y names 
# for upper x > y, lower x < y 
  lst <- list(...) 

  n <- ncol(corrPlot) 
  nms <- colnames(corrPlot) 
  colnames(corrPlot) <- if(is.null(nms)) 1:ncol(corrPlot) else nms 

  xleft <- match(lst$x, colnames(corrPlot)) - 0.5 
  ybottom <- n - match(lst$y, colnames(corrPlot)) + 0.5 

  lst <- list(xleft=xleft, ybottom=ybottom, xright=xleft+1, ytop=ybottom+1) 
  do.call(rect, c(lst, rectArgs)) 
}

corrplotCellBorders <- function(plt, mark.ids) {
  x <- mark.ids$x
  y <- mark.ids$y
  cb(plt, x, y, rectArgs=list(border="red", lwd=3))
}

打开

  • 如何创建 mark.ids 以便您可以通过 mark.ids$xmark.ids$y 调用其项目?
  • 为上三角整合点序中性