ggcorrplot2 shows different significance asterisks - r

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)

Related

P-Value Representation Using corrplot()

The code I have written produces the image attached here. I'm wanting to make the asterisks smaller in size, but I am not sure how to do this. Also, is it possible to represent different levels of significance with the number of asterisks (*=0.1, **=0.05, ***=0.001). Thanks in advance. My code and figure are below as well.
alpha_corr <- cor(Schiller_Numerical, method = "pearson")
alpha_p <- cor.mtest(alpha_corr, method = "pearson", conf.level = 0.95, exact = FALSE)
corrplot(
cor(alpha_corr, method = "pearson"),
tl.col = "black",
tl.srt = 60,
method = 'square',
order = 'hclust',
type = 'lower',
diag = TRUE,
tl.cex = 1,
title = "Heatmap For All Data",
mar=c(0,0,2,0),
p.mat = alpha_p$p,
sig.level = 0.05,
insig = "label_sig") `
You can find all the useful parameters you required in the corrplot() documentation : https://www.rdocumentation.org/packages/corrplot/versions/0.92/topics/corrplot
You need to modify pch.cex to adjust asdterisk size, and sig.level to specify the signficance levels as a vector.
data(mtcars)
alpha_p <- cor.mtest(mtcars, method = "pearson", conf.level = 0.95, exact = FALSE)
corrplot(
cor(mtcars, method = "pearson"),
tl.col = "black",
tl.srt = 60,
method = 'square',
order = 'hclust',
type = 'lower',
diag = TRUE,
tl.cex = 1,
title = "Heatmap For All Data",
mar=c(0,0,2,0),
p.mat = alpha_p$p,
sig.level = c(0.001,0.05,0.1),
insig = "label_sig",
pch.cex=1)

P-value significance levels in corrplot

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!

Is it possible to add a grouping label to a corrplot?

Is it possible to add a label that applies to a few variables in a corrplot? This is an example of what I'm after. In that case it is the word "Petals" on the y-axis.
Below is the code to make the rest of the plot.
library(corrplot)
library(Hmisc)
iris_c <- iris[, c(1:4)]
test <- rcorr(as.matrix(iris_c))
corrplot(test$r, type="lower", order= "original",
p.mat = test$P, sig.level = 0.01, insig = "blank",
col= colorRampPalette(c("blue","white", "red"))(100), tl.col = 'black', cl.ratio = 0.8, tl.srt = 45, diag = FALSE, method = "square")
You can use title() to adjust axis labels, which is what you're looking for here. After running your code above, just run:
title(ylab = "Petals")

how to show significant and insignificant coeficients in a corrplot when use insig equall 'blank"?

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)

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, 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)

Resources