R corrplot:绘制相关系数和重要性星?

R corrplot: Plot correlation coefficients along with significance stars?

使用 R corrplot,我还没有找到将方框中的相关系数与其显着性一起绘制的解决方案,即 0.84*** 这是仅绘制重要星星的代码。那里怎么加上相关系数?

M<-cor(mtcars)
res1 <- cor.mtest(mtcars, conf.level = .95)
corrplot(cor(mtcars),
     method="square",
     type="lower",
     p.mat = res1$p,
     insig = "label_sig",
     sig.level = c(.001, .01, .05),
     pch.cex = 0.8,
     pch.col = "red",
     tl.col="black",
     tl.cex=1,
     outline=TRUE)

如果我按照第一个答案的建议添加 addCoef.col = "black", 文字覆盖了重要的星星,因此它们再也看不到了:

您只需将选项 addCoef.col = "black" 添加到 corrplot

为了解决星星的颜色冲突问题,我采用了这种方法,让更大的星星变成白色。我将其与以下颜色和布局一起使用。我无法避免重叠,但它足够清晰,我可以使用:

cex.before <- par("cex")
par(cex = 0.7)
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
corrplot::corrplot(cor(mtcars), 
    method="color", 
    col=col(200),  
    type="lower", 
    # Combine with significance
    p.mat = res1$p, 
    insig = "label_sig",
    sig.level = c(.001, .01, .05), 
    pch.cex = 3, # Increase size of stars
    pch.col = "white", # Colour of stars
    # hide correlation coefficient on the principal diagonal
    diag=FALSE,
    addCoef.col = "black", # Add coefficient of correlation
    tl.col="black", tl.srt=45, #Text label color and rotation
    tl.cex = 1/par("cex"), cl.cex = 1/par("cex") #Reduce text size of coefficients               
)
par(cex = cex.before)

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

问题:

如果同时显示相关系数和显着性水平,它们会重叠(我用黄色表示星星,因为我的色觉有些问题...)。

library(corrplot)
#> corrplot 0.90 loaded

M<-cor(mtcars)
res1 <- cor.mtest(mtcars, conf.level = .95)

corrplot(cor(mtcars),
         method="square",
         type="lower",
         p.mat = res1$p,
         insig = "label_sig",
         sig.level = c(.001, .01, .05),
         pch.cex = 0.8,
         pch.col = "yellow",
         tl.col="black",
         tl.cex=1,
         addCoef.col = "black",
         tl.pos="n",
         outline=TRUE)

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)

然后点击“保存”按钮。

结果:

library(corrplot)
#> corrplot 0.90 loaded

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

M<-cor(mtcars)
res1 <- cor.mtest(mtcars, conf.level = .95)

corrplot(cor(mtcars),
         method="square",
         type="lower",
         p.mat = res1$p,
         insig = "label_sig",
         sig.level = c(.001, .01, .05),
         pch.cex = 0.8,
         pch.col = "yellow",
         tl.col="black",
         tl.cex=1,
         addCoef.col = "black",
         tl.pos="n",
         outline=TRUE)

reprex package (v2.0.1)

于 2021-10-13 创建