R corrplot:绘制相关系数和显着性星号?

问题描述 投票:0回答:4

使用 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”, 文本覆盖了重要的星星,因此它们实际上不再可见: enter image description here

r correlation significance
4个回答
10
投票

重要性星星的位置由

place_points
函数内的
corrplot
函数定义。

问题:

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

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 包于 2021 年 10 月 13 日创建(v2.0.1)

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

更改

place_points
函数内的
corrplot
函数。为此,请运行:

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 包于 2021 年 10 月 13 日创建(v2.0.1)


1
投票

您只需将选项

addCoef.col = "black"
添加到
corrplot


1
投票

为了解决星星的颜色冲突,我采用了这种方法,使更大的星星具有白色。我将其与以下颜色和布局一起使用。我无法避免重叠,但它对于我来说足够清晰:

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)

0
投票

如果您的唯一目标是用最少的编码实现相同的情节。看看metan

    library(metan)
    c <- corr_coef(mtcars)

    par(family="serif")
    grDevices::windows(16, 10)
    p <- plot(c,
              col.low = "#2166AC",
              col.mid = "white",
              col.high = "#B2182B")+
      theme(text=element_text(family="serif", size=12), )
    p

输出图像

© www.soinside.com 2019 - 2024. All rights reserved.