如何着色R corrplot中的一些细胞边界? [英] How to colourise some cell borders in R corrplot?

查看:3147
本文介绍了如何着色R corrplot中的一些细胞边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保持一些细胞的注意,使他们的边界明显不同于任何其他。
参数 rect.col 用于对所有边框进行着色,但只想对单元格(3,3​​)和(7,7)的边框进行着色,例如通过任何光晕颜色等 heat.colors(100) rainbow(12)
代码

I would like to keep some cells in attention by making their borders clearly distinct from anything else. The parameter rect.col is used to colorise all borders but I want to colorise only borders of the cells (3,3) and (7,7), for instance, by any halo color etc heat.colors(100) or rainbow(12). Code

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)处产生伪影后输出,

Fig。 3输出窗口大小修正

Fig. 1 Output of the top code without cell bordering, Fig. 2 Output after manually converting all coordinates to upper triangle but artifact at (10,1),
Fig. 3 Output with window size fix


输入:locations by ids (3,3)(7,7)

预期输出:上面三角形上标记边框的两个单元格

Input: locations by ids (3,3) and (7,7)
Expected output: two cells where borders marked on upper triangle

伪代码

# 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的提案



Testing user20650's proposal

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

2.
这将是一个很好的功能。
假设你有一个ID列表。

Output in Fig. 2. It would be great to have a function for this. Assume you have a list of IDs.

我认为展示位置有问题,因为(2,3),(9,10)通向(2,3),(2, 3)。

I think there is something wrong with the placement because (2,3),(9,10) leads to the point in (2,3),(2,3).

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

输出,其中在图1中仅标记一个单元边界。

Output where only one cell border marked in Fig. 3.

预期输出:标有三个单元格边框

Expected output: three cell borders marked

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

You have to work all coordinates first to upper triangle. So you can now call only the following where output has an artifact at (10,1) in Fig. 2

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

预期输出: at(10,1)

Expected output: no artifact at (10,1)

工件的原因可以是白色背景,但如果边框颜色 red 所以很可能这不是原因。
解决方案 - 修正窗口大小及其输出。 3

The cause of the artifact can be white background, but it occurs also if the border color is red so most probably it is not the cause. Solution - fix the window size and its output in Fig. 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()


$ b b

R:3.3.1

操作系统:Debian 8.5

文件编辑:这里

推荐答案

code> mark.ids 。我发现最好有 plt mark.ids 作为 corrplotCellBorders

My proposal where still pseudocode mark.ids. I found best to have plt and mark.ids as the options of corrplotCellBorders which creates corrplot with bordered wanted cells

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 http://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 $ x mark.ids $ y

  • 集成上三角的点单中立性这里

  • How to create mark.ids such that you can call its items by mark.ids$x and mark.ids$y?
  • Integrate point order neutrality for the upper triangle here

这篇关于如何着色R corrplot中的一些细胞边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆