Add significant correlations to corrplot graph in R - r

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

Related

Add hierarchical clustering to correlation plot?

I've generated correlation heatmaps using the following code:
corrplot(data, method = "color",
type = "lower", order = "hclust", col=colorRampPalette(c("blue","white","red"))(400), tl.col = 'black',
cl.ratio = 0.2, tl.srt = 45)
I also have hierarchical clustering results from pvclust and would like to add those results to the correlation plot so that each cluster is grouped together by a box on the correlation plot. Does anyone know how to do this? Thanks!!

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!

ggpairs: Plotting only the first two rows of a correlation matrix

I'd like to do some correlation analysis with plotting. As my actual data is too large I used the mtcars dataframe to setup an example.
Here the code
library(ggplot2)
library(ggcorrplot)
mtcars
library(ggcorrplot)
# Computing correlation matrix
corrmatr_mtcars <- round(cor(subset(mtcars[c(3:7,1)])),1)
head(corrmatr_mtcars[,1:6])
corrmatr_mtcars
# Computing correlation matrix with p-values
corrmatr_mtcars.mat <- cor_pmat(mtcars[c(3:7,1)])
head(corrmatr_mtcars.mat[, 1:6])
corrmatr_mtcars.mat
library(GGally)
ggpairs(mtcars[c(3:7,1)],
title = "Corr Analysis of...",
lower = list(continuous = wrap("cor",
size = 3)),
upper = list(
continuous = wrap("smooth",
alpha = 0.3,
size = 0.1))
)
With this plot result:
But, I am interested only in the correlation of the first two variables against all others. So, for avoiding unneccessary information and saving place I'd rather like
my plot to show only the first two correlation rows. All other correlations could be dropped.
In the end, I imagine something as follows needing only 3 rows.
Subsequently the Corr-Value labels should be placed at the scatterplot panels.>br>
I couldn't find any option to do so.
Would that even generally be possible with ggpairs (without complex functions)? If yes: how? If no: what could be an approach with a comparable result?
It can be done this way
library(ggplot2)
library(ggcorrplot)
mtcars
library(ggcorrplot)
# Computing correlation matrix
corrmatr_mtcars <- round(cor(subset(mtcars[c(3:7,1)])),1)
head(corrmatr_mtcars[,1:6])
corrmatr_mtcars
# Computing correlation matrix with p-values
corrmatr_mtcars.mat <- cor_pmat(mtcars[c(3:7,1)])
head(corrmatr_mtcars.mat[, 1:6])
corrmatr_mtcars.mat
library(GGally)
gg1 = ggpairs(mtcars[c(3:7,1)],
title = "Corr Analysis of...",
lower = list(continuous = wrap("cor",
size = 3)),
upper = list(
continuous = wrap("smooth",
alpha = 0.3,
size = 0.1))
)
gg1$plots = gg1$plots[1:12]
gg1$yAxisLabels = gg1$yAxisLabels[1:2]
gg1

How to make correlation matrix with especific columns in R

I have a multi-column dataframe that can be divided into two categories: land use and water quality.
I would like to analyze the correlation only between water quality variables and land use variables, without there being a correlation between land use variables and a correlation between quality variables.
I am using package corrplot() spearmen method but i dont know how i can ignore correlation between categories.
Landuse = veg, wet, dry, water
Quality water = OD, DBO, DQO
library(ggplot2)
library(dplyr)
library(corrplot)
veg<-c(1,2,3,2.3,4.1)
wet<-c(2,2.3,1.9,2.5,2.2)
dry<-c(5,5.1,6.9,4.3,5.3)
water<-c(0.69,0.75,0.81,0.82,0.82)
coli<-c(10,11,12,13,9.7)
OD<-c(1,3,2.5,2.7,1.8)
DBO<-c(7,8,9,6.5,8)
DQO<-c(3.5,4,4.1,3,2)
#landuse=veg, wet, dry, water
#quality water = OD, DBO, DQO
data_land<-data.frame(veg, wet, dry, water, OD, DBO, DQO)
correl<-corrplot(cor(as.matrix(data_land),method = "spearman"),
method = "color",
tl.cex = 0.9,
number.cex = 0.95,
addCoef.col = "black")
For example, I don't want the correlation between DQO and DBO to be calculated, as they are quality parameters. I also don't want the correlation between veg and dry to be calculated, as they are land use classes, for example.
You can calculate your Spearman correlations between land and water variables by selecting columns when calling cor. The function cor can alternatively accept a second matrix to compute correlations - so you can compute correlations between a "land use" matrix (columns 1-4) and a "water quality" matrix (columns 5-7):
my_cor <- cor(data_land[, 1:4],
data_land[, 5:7],
method = "spearman")
corrplot(my_cor,
method = "color",
tl.cex = 0.9,
number.cex = 0.95,
addCoef.col = "black")
Plot
If you instead want a square correlation plot, with empty spaces where you did not want to show correlations, you could try calculating correlations as you had before, set correlation results to NA for those you want to hide, and then set na.label to an empty space in corrplot:
my_cor <- cor(data_land, method = "spearman")
my_cor[1:4, 1:4] <- NA
my_cor[5:7, 5:7] <- NA
corrplot(my_cor,
na.label = " ",
method = "color",
tl.cex = 0.9,
number.cex = 0.95,
addCoef.col = "black")

How can I display the p-value in a correlation Plot in the right size?

I have plotted a correlation matrix resulting from the function "rcorr" with "corrplot". Everything comes out fine so far, except one thing. I would like to display the p-value for either all, or for at least the cells, where the p-value is higher than the previously defined significance level of 0.005. The "insig" function does display the the p-values for the cells with p<sig, but I can't change the size of the plotted numbers. I tried to use any of the given arguments of the "corrplot" function, but it has no effect of the size of the p-value text. Does anyone know how I could change the size and how I can control the position of the p-values in each cell?
Here is the code I am using:
my_data <- FS[,2:27]
# Mark the insignificant coefficients according to the specified p-value significance level
library(corrplot)
library(Hmisc)
FScor <- rcorr(as.matrix(my_data[,1:25],method = c("spearman")))
M_Konz <- FScor$r
p_matK <- FScor$P
CorrKonz <- corrplot(M_Konz, type = "upper", order = "hclust", insig = "p-value",
p.mat = p_matK, sig.level = 0.005,
tl.cex = 0.7,tl.col = "black",
title = "")
Unfortunately corrplot does not give you direct control over the size of the p-values, but you can get it by setting the graphics parameter directly. You did not provide reproducible data so I'll use an example from the corrplot vignette. Your code has a problem with the rcorr line since method= is not an argument of as.matrix or rcorr.
M <- rcorr(as.matrix(mtcars), type="spearman")
Mr <- M$r
Mp <- M$P
oldp <- par(cex=.75)
Mcorr <- corrplot(Mr, type = "upper", order = "hclust", insig = "p-value",
p.mat = Mp, sig.level = 0.005, tl.cex = 1.25, tl.col = "black",
cl.cex=1.25, title = "")
par(oldp)
The mtcars data frame is included with R. We set the graphical parameter cex to .75 (default is 1) and then increase the size of the variable labels with tl.cex and the legend labels with cl.cex. After drawing the plot we reset the default graphics parameters.

Resources