Add p-values in corrplot matrix
Asked Answered
M

3

5

I calculated the Spearman correlation between two matrices and I'm plotting the r values using corrplot. How can I plot only the significant correlations (so only those correlations having p value lower than 0.00 and delete those having higher p value, even if are strong correlations - high value of r). I generated the correlation matrix using corr.test in psych package, so I already have the p values in cor.matrix$p

This is the code I'm using:

library(corrplot)
library(psych)
corr.test(mydata_t1, mydata_t2, method="spearman")
M <- corrplot(cor.matrix$r, method="square",type="lower",col=col1(100),is.corr=T,mar=c(1,1,1,1),tl.cex=0.5)

How can I modify it to plot only significant corelations?

Mullen answered 15/1, 2014 at 20:17 Comment(2)
What do you want to do with the cells where correlations are greater than zero?Jaynajayne
Maybe I didn't explain well... I want to plot positive and negative correlations, with a p value < 0.05. If p>0.05, I want a white cell...Mullen
O
8

Take a look at the examples of corrplot. do ?corrplot. It has options for doing what you want. You can plot the p-values on the graph itself, which I think is better than putting stars, as people not familiar with that terminology have one more thing to look up. to put p-values on graph do this corrplot(cor.matrix$r, p.mat = cor.matrix$p, insig = "p-value") where cor.matrix is object holding the result of cor.test. The insig option can put:

  • p-values (as shown above)
  • blank out insignificant correlations with corrplot(cor.matrix$r, p.mat = cor.matrix$p, insig = "blank")`
  • Cross out (put a X on) insignificant correlations) with option corrplot(cor.matrix$r, p.mat = cor.matrix$p, insig = "pch") (DEFAULT)
  • Do nothing with to the plot, with corrplot(cor.matrix$r, p.mat = cor.matrix$p, insig = "n")

If you do want stars, p-value on the correlation matrix plot - take a look at this thread Correlation Corrplot Configuration

Though I have to say I really like @sven hohenstein's elegant subset solution.

Orthoepy answered 15/1, 2014 at 21:57 Comment(0)
J
4

Create a copy of cor.mat and replace the corresponding correlation coefficients with zero:

cor.matrix2 <- cor.matrix

# find cells with p-values > 0.05 and replace corresponding
# correlations coefficients with zero
cor.matrix2$r[cor.matrix2$p > 0.05] <- 0

# use this matrix for corrplot
M <- corrplot(cor.matrix2$r, method="square",type="lower",col=col1(100),
              is.corr=T,mar=c(1,1,1,1),tl.cex=0.5)

The replaced values will appear as a white cell.

Jaynajayne answered 15/1, 2014 at 20:58 Comment(2)
And if I want to plot all the corelations, but add a star in the cells with a significant correlation?Mullen
@FrancescadeFilippis This another problem. You should ask a new question.Jaynajayne
M
1

What you are asking is similar to what subset does:

Return subsets of vectors, matrices or data frames which meet conditions.

So you can do:

cor.matrix <- subset(cor.matrix, p<0.00)
P <- corrplot(cor.matrix$r, method="square",type="lower",col=col1(100),is.corr=T,mar=c(1,1,1,1),tl.cex=0.5)
Metrify answered 15/1, 2014 at 20:42 Comment(1)
Maybe now works (I edited the question)but the other two answers are better and taking into account the package functionalities.Metrify

© 2022 - 2024 — McMap. All rights reserved.