I would like to show not only * when a correlation is significant but the different significance levels, e.g. * for p < .05, ** for p < 0.01 and so on.
I calculated a correlation and the p-value-function as follows:
Corr_Emo <- cor(df_Emo[, 3:11], method = "pearson")
pval <- psych::corr.test(df_Emo[3:11], adjust="none")$p
and plotted my corrplot
corrplot(Corr_Emo,
method="color", col=col(200),
tl.srt = 45,
tl.cex = 1.0,
tl.col = "black",
diag=FALSE,
type="upper", #order="hclust",
title=title,
addCoef.col = "black",
# Combine with significance
p.mat = pval, sig.level = 0.05, insig="label_sig", pch.cex = 1.5,
mar=c(0,0,1,0)
)
but of course it only signs a correlation * if it is significant in any way.
I am not that good in R so I don't know exactly how to modify pval with psych::corr.test in a way that it shows me the different levels of *.
Could anybody help me here?
Thank you very much in advance!
Related
I'm using this code:
corrplot(v_cor,
method="color",
col = c('#bf9000ff','#f1c232ff','#ffe599ff','#fff2ccff','#c9daf8ff','#a4c2f4ff','#3c78d8ff','#21437aff'),
type="lower", order="original",
addCoef.col = "black", # Add coefficient of correlation
tl.col="black", tl.srt=45, #Text label color and rotation
# Combine with significance
p.mat = p_val, sig.level = 0.1, insig = "blank",
# hide correlation coefficient on the principal diagonal
diag=FALSE )
I have this result:
1st result
But I want to show the insignificant coefficients but the box must be blank.
e.g.
desired result
UPDATE:
I was able to solve this problem and added one more line.
corrplot(v_cor,
method="color",
col = c('#bf9000ff','#f1c232ff','#ffe599ff','#fff2ccff','#c9daf8ff','#a4c2f4ff','#3c78d8ff','#21437aff'),
type="lower", order="original",
addCoef.col = "black", # Add coefficient of correlation
tl.col="black", tl.srt=45, #Text label color and rotation
# Combine with significance
p.mat = p_val, sig.level = 0.1, insig = "blank",
# hide correlation coefficient on the principal diagonal
diag=FALSE ) -> p1
text(p1$x, p1$y,round(p1$corr, 2) ,cex= 0.8, font = 2)
does anybody have an idea why ggcorrplot2 shows different significance asterisks than ggcorrplot? Very confusing to me.
https://github.com/caijun/ggcorrplot2
data(mtcars)
ct <- corr.test(mtcars)
corr <- ct$r
p.mat <- ct$p
ggcorrplot(corr, type= "lower", p.mat = p.mat,
insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001),show.diag=F)
ggcorrplot.mixed(corr, upper = "number", lower = "circle", p.mat = p.mat,
insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001))
Update: Ok I think I figured it out finally. Its because corr.test() writes a unsymmetric matrix of p.values.
"Entries above the diagonal are adjusted for multiple tests."
I fixed this with p.mat[lower.tri(p.mat)] <- t(p.mat)[lower.tri(p.mat)].
Furthermore if you want to use the adjusted p.Values it is important to mirror the triangle above the diagonal of the p.Value matrix. If you need the unadjusted p.Values it is the lower triangle that needs to be mirrored (code needs to be changed accordingly).
data(mtcars)
cor.matrix <- corr.test(mtcars,method = "spearman", adjust = "BH", alpha = 0.05, ci = F)
corr <- cor.matrix[["r"]]
p.mat <- cor.matrix[["p"]]
p.mat[lower.tri(p.mat)] <- t(p.mat)[lower.tri(p.mat)] #to get only the adjusted p.Values symmetrically over the plot
p.mat[lower.tri(p.mat, diag = T)] <- 1 #to set the lower triangle to 1
corrplot.mixed(corr, order= "original",mar=c(0,0,2,0), tl.col = 'black', p.mat = p.mat, insig = "label_sig", sig.level = c(.001, .01, .05), pch.cex=1.5, tl.cex = .8, number.font=2, number.cex=0.8)
data(mtcars)
cor.matrix <- corr.test(mtcars,method = "spearman", adjust = "BH", alpha = 0.05, ci = F)
corr <- cor.matrix[["r"]]
p.mat <- cor.matrix[["p"]]
p.mat[lower.tri(p.mat)] <- t(p.mat)[lower.tri(p.mat)] #to get only the adjusted p.Values symmetrically over the plot
p.mat[lower.tri(p.mat, diag = T)] <- 1 #to set the lower triangle to 1 (this way the asterisks wont be displayed on this part of the graph)
corrplot.mixed(corr, order= "original",mar=c(0,0,2,0), tl.col = 'black', p.mat = p.mat, insig = "label_sig", sig.level = c(.001, .01, .05), pch.cex=1.5, tl.cex = .8, number.font=2, number.cex=0.8)
For my data I made a correlation plot. I want to visualize the correlations with the color method. Additionaly I would like to add the correlation coefficient, however, I only want to visualize them for the significant correlations, in all other cases I would like to show a cross.
This is what I have done:
GM_young <- replicate(6, rnorm(11))
colnames(GM_young2) <- c('PA_rest','FL-rest','MW_rest','PA_con','FL_con', 'MW_con')
corrGM_young<-cor(GM_young) # gives you a matrix with correlations
p.mat <- cor.mtest(GM_young) # matrix of the p-value of the correlation
# make corrplot
corrplot(corrGM_young, method = "color", type="lower",order = "hclust", p.mat = p.mat, sig.level = 0.01, diag=FALSE , tl.col="black",tl.cex = 0.8, cl.cex = 0.8,cl.ratio=0.3,tl.srt = 45, title="Gastrocnemius Young", mar=c(0,0,2,0), addCoef.col = TRUE)
However, now I get the coefficients for all the correlations. I only want to visualize the 0.74. The other boxes I want to visualize using the colors and the cross as they are not significant.
Can someone help me with this? Thank you
Using R corrplot, I have not found a solution where the correlation coefficients in the boxes are plotted together with their significances, i.e. 0.84***
Here is the code plotting only the significance stars. 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:
The position of the significance stars is defined by the place_points function within the corrplot function.
Problem:
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)
Quick and temporary (you have to re-do this step everytime you newly loaded the corrplot package) solution:
Change the place_points function within the corrplot function. To do so, run:
trace(corrplot, edit=TRUE)
Then replace on line 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.
Result:
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)
You only have to add the option addCoef.col = "black" to 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)
I am desperately trying to solve an issue with R corrplot. I have a 10 * 22 matrix that I would like to visualize using corrplot, but adding significance level just produces crosses outside of the boarder of the visual. I could produce correlation plots without significant tests using the following script:
corrplot(matrix, method = "circle",tl.cex=0.4, tl.col = "black", col = color(10))
However, the following script produces crosses outside of the border of the visual:
res1 <- cor.mtest(corredata, conf.level = .95)
corrplot(matrix, p.mat = res1$p, method = "circle", tl.cex=0.6, tl.col = "black",
col = color(10), sig.level = 0.05, insig = "pch")
Is there something wrong with my code?