【问题标题】:How to colourise some cell borders in R corrplot?如何在 R corrplot 中为某些单元格边框着色?
【发布时间】:2017-03-25 02:42:38
【问题描述】:

我想通过使它们的边界与其他任何东西明显不同来保持一些单元格的注意力。

参数rect.col 用于着色所有边框,但我只想着色单元格(3,3​​)和(7,7)的边框,例如,任何光晕颜色等heat.colors(100) 或@987654331 @。

代码:

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 修正窗口大小的输出

输入: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 http://stackoverflow.com/q/40538304/54964 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 也会发生,所以很可能不是原因。 解决方案 - 修复图 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
操作系统:Debian 8.5
文档 corrplot:here

【问题讨论】:

  • 我认为您可能必须手动执行此操作,即 rect(2+0.5, 9+0.5, 3+0.5, 10+0.5, border="white", lwd=2) 。当然,如果您知道单元格的数量、上/下三角形、其他设置,您可以自动执行此操作
  • 这是一个艰难的开始;看看你能不能为你做点什么chat.stackoverflow.com/rooms/127905/masi-qa
  • @user20650 请参阅下面的 wiki 答案。还有一些伪代码我做不到,但我知道我想要什么。

标签: r colors border correlation r-corrplot


【解决方案1】:

我的建议仍然是伪代码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 https://stackoverflow.com/q/40538304/54964 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 调用其项目?
  • 为上三角形整合点顺序中立性here

【讨论】:

    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多