[英]R corrplot: Plot correlation coefficients along with significance stars?
Using R corrplot, I have not found a solution where the correlation coefficients in the boxes are plotted together with their significances, ie 0.84*** Here is the code plotting only the significance stars.使用 R corrplot,我还没有找到将方框中的相关系数与其重要性一起绘制的解决方案,即 0.84*** 这是仅绘制重要性星的代码。 How can the correlation coefficients be added there?如何在那里添加相关系数?
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)
If I add, as by the first answer suggested, addCoef.col = "black", the text overlays the significance stars so they cannot really be seen anymore:如果我按照第一个答案的建议添加 addCoef.col = "black",则文本会覆盖重要星,因此它们不再被真正看到:
You only have to add the option addCoef.col = "black"
to corrplot
您只需将选项addCoef.col = "black"
添加到corrplot
To fix the colour clash of the stars I adapted this approach to have bigger stars with a white colour.为了解决星星的颜色冲突,我采用了这种方法来制作更大的白色星星。 I use this with the following colours and layout.我将它与以下颜色和布局一起使用。 I can't avoid the overlap but it is legible enough for me to work with:我无法避免重叠,但它足够清晰,我可以使用:
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)
The position of the significance stars is defined by the place_points
function within the corrplot
function.重要星的 position 由place_points
function 中的corrplot
function 定义。
If both, the correlation coefficients and the significance level should be displayed, they overlap (I took yellow for the stars since I have some issues with colour vision...).如果两者都应该显示相关系数和显着性水平,它们就会重叠(我把星星选为黄色,因为我有一些色觉问题......)。
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)
Created on 2021-10-13 by the reprex package (v2.0.1)由代表 package (v2.0.1) 于 2021 年 10 月 13 日创建
Change the place_points
function within the corrplot
function.更改place_points
function 中的corrplot
function。 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.然后点击“保存”按钮。
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)
Created on 2021-10-13 by the reprex package (v2.0.1)由代表 package (v2.0.1) 于 2021 年 10 月 13 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.