【问题标题】:Changing the position of the signifigance pch symbols in `corrplot()`?更改 `corrplot()` 中显着性 pch 符号的位置?
【发布时间】:2018-09-16 14:51:24
【问题描述】:

下面的脚本会生成一个绘图,其中用于显着性的 pch 符号与 r 值重叠。如何移动 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")

最好能自动完成,但也可以手动完成:

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

【问题讨论】:

  • 如果能在右上角就太好了

标签: r plot r-corrplot


【解决方案1】:

重要性星的位置由 corrplot 函数中的 place_points 函数定义。

问题:

如果同时显示相关系数和显着性水平,则它们重叠。

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

reprex package (v2.0.1) 于 2021 年 10 月 13 日创建

快速且临时(每次新加载 corrplot 包时都必须重新执行此步骤)解决方案:

corrplot 函数中更改place_points 函数。为此,请运行:

trace(corrplot, edit=TRUE)

然后在第 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)

与:

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

然后点击“保存”按钮。

结果:

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

reprex package (v2.0.1) 于 2021 年 10 月 13 日创建

【讨论】:

    猜你喜欢
    • 2017-09-03
    • 1970-01-01
    • 2018-08-14
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 2017-03-14
    相关资源
    最近更新 更多