简体   繁体   English

更改“corrplot()”中显着性 pch 符号的位置?

[英]Changing the position of the signifigance pch symbols in `corrplot()`?

The script below produces a plot in which the pch symbols for significance overlap the r values.下面的脚本生成一个图,其中表示显着性的 pch 符号与 r 值重叠。 How do you shift the position of the pch symbols so that they do not overlap these values?如何移动 pch 符号的位置,使它们不与这些值重叠?

library(corrplot)

ex.mat <- matrix(c(1.00,0.46,-0.75,1.00,0.46,1.00,0.00,0.46,-0.75,0.00,1.00,-0.75,1.00,0.46,-0.75,1.00), nrow = 4, ncol = 4)
ex.pmat <- matrix(c(NA,0.2939,0.0522,0.0000,0.2939,NA,1.0000,0.2939,0.0522,1.0000,NA,0.0522,0.0000,0.2939,0.0522,NA), nrow = 4, ncol = 4)

corrplot(ex.mat, p.mat = ex.pmat ,sig.level = c(.001, .01, .05), type = "upper", 
         insig = "label_sig", pch.cex = 1.5,
         tl.col = "black", method = "color", tl.srt = 28, number.cex = 1, tl.cex = 1,  addCoef.col = "dodgerblue",
         pch.col = "tomato", font.main = 4, family = "serif", mar=c(0,0,1,0), cl.pos = "b")

It would be ideal to be able to automate this, but it can be done manually:能够自动执行此操作是理想的,但可以手动完成:

ex.mat <- matrix(c(1.00,0.46,-0.75,1.00,0.46,1.00,0.00,0.46,-0.75,0.00,1.00,-0.75,1.00,0.46,-0.75,1.00), nrow = 4, ncol = 4)
ex.pmat <- matrix(c(NA,0.2939,0.0522,0.0000,0.2939,NA,1.0000,0.2939,0.0522,1.0000,NA,0.0522,0.0000,0.2939,0.0522,NA), nrow = 4, ncol = 4)

corrplot(ex.mat, type = "upper", 
         insig = "label_sig", pch.cex = 1.5, cl.length = 3,
         tl.col = "black", method = "color", tl.srt = 28, number.cex = 1, tl.cex = 1,  addCoef.col = "dodgerblue",
         pch.col = "tomato", font.main = 4, family = "serif", mar=c(0,0,1,0), cl.pos = "b")


points(4.35, 4.25 , type = "p", pch = "*", cex = 2, col = "ivory")
points(4.20, 4.25 , type = "p", pch = "*", cex = 2, col = "ivory")
points(4.05, 4.25 , type = "p", pch = "*", cex = 2, col = "ivory")

The position of the significance stars is defined by the place_points function within the corrplot function.重要星的位置由 corrplot 函数中的 place_points 函数定义。

Problem:问题:

If both, the correlation coefficients and the significance level are displayed, they overlap.如果同时显示相关系数和显着性水平,则它们会重叠。

# library
library(corrplot)
#> corrplot 0.90 loaded

# data
ex.mat <- matrix(c(1.00,0.46,-0.75,1.00,0.46,1.00,0.00,0.46,-0.75,0.00,1.00,-0.75,1.00,0.46,-0.75,1.00), nrow = 4, ncol = 4)

#since your example threw an error with the actual corrplot package I slightly edited you code

#set colnames
colnames(ex.mat) <- c("A","B","C","D")

# calculate p-values
ex.pmat <- cor.mtest(ex.mat, conf.level = .95)

# overlapping plot
corrplot(ex.mat, p.mat = ex.pmat$p ,sig.level = c(.001, .01, .05), type = "upper", 
         insig = "label_sig", pch.cex = 1.5,
         tl.col = "black", method = "color", tl.srt = 28, number.cex = 1, tl.cex = 1,  addCoef.col = "dodgerblue",
         pch.col = "tomato", font.main = 4, family = "serif", mar=c(0,0,1,0), cl.pos = "b")

Created on 2021-10-13 by the reprex package (v2.0.1)reprex 包(v2.0.1) 于 2021 年 10 月 13 日创建

Quick and temporary (you have to re-do this step everytime you newly loaded the corrplot package) solution:快速且临时(每次新加载 corrplot 包时都必须重新执行此步骤)解决方案:

Change the place_points function within the corrplot function.place_points函数中更改corrplot函数。 To do so, run:为此,请运行:

trace(corrplot, edit=TRUE)

Then replace on line 443然后在第 443 行替换

place_points = function(sig.locs, point) {
  text(pos.pNew[, 1][sig.locs], pos.pNew[, 2][sig.locs], 
       labels = point, col = pch.col, cex = pch.cex, 
       lwd = 2)

with:和:

# adjust text(X,Y ...) according to your needs, here +0.25 is added to the Y-position    
place_points = function(sig.locs, point) {
      text(pos.pNew[, 1][sig.locs], (pos.pNew[, 2][sig.locs])+0.25, 
           labels = point, col = pch.col, cex = pch.cex, 
           lwd = 2)

and then hit the "Save" button.然后点击“保存”按钮。

Result:结果:

# chance the corrplot function as described above
trace(corrplot, edit=TRUE)
#> Tracing function "corrplot" in package "corrplot"
#> [1] "corrplot"

# non-overlapping plot
corrplot(ex.mat, p.mat = ex.pmat$p ,sig.level = c(.001, .01, .05), type = "upper", 
         insig = "label_sig", pch.cex = 1.5,
         tl.col = "black", method = "color", tl.srt = 28, number.cex = 1, tl.cex = 1,  addCoef.col = "dodgerblue",
         pch.col = "tomato", font.main = 4, family = "serif", mar=c(0,0,1,0), cl.pos = "b")

Created on 2021-10-13 by the reprex package (v2.0.1)reprex 包(v2.0.1) 于 2021 年 10 月 13 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM