R相关图:绘制相关系数和重要恒星? [英] R corrplot: Plot correlation coefficients along with significance stars?

查看:19
本文介绍了R相关图:绘制相关系数和重要恒星?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用R corrlot,我没有找到一个解决方案,其中框中的相关系数与它们的重要性一起绘制,即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.ol=&Black";, 文字覆盖了重要的星星,因此它们不能再被真正地看到:

推荐答案

重要星的位置由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创建

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

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

,然后单击"保存&q;"按钮。

结果:

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创建

这篇关于R相关图:绘制相关系数和重要恒星?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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