# Heatmap correlation with pvalues

Hi,

i'm looking for code or ideas to draw correlation heatmap (spearman) with pvalue significances (*, **, ***), such as the picture i send here.

thank you --

There are 3 questions in here:

1. how to get the Spearman correlation coefficients, you can use cor(x, y, method = "spearman")
2. how to compute the p-values, that will depend on your context, the most natural one is probably cor.test(x, y, method = "spearman")
3. how to how to draw a heatmap with the stars, here you can use pheatmap() in the eponymous package, and use display_numbers = matrix where you precompute a matrix with the appropriate number of stars.

Of course since you have a lot of computations to make, you might need to use a for loop or similar to do all these, the easiest approach depends on what your data looks like.

Here is an example code (generic code though, you may need to change the test etc to suit your situation):

library(pheatmap)
set.seed(123)

# create some fake data
nb_samples <- 50
metabolites <- data.frame(LysoPC = runif(nb_samples, 0, 100),
PE = runif(nb_samples, 0, 100))

echocardiographic_parameters <- data.frame(LVEDD = 5 * metabolites\$LysoPC + rnorm(nb_samples,sd = .01),
LVEF = 2 * metabolites\$PE - 3 * metabolites\$LysoPC + rnorm(nb_samples, sd = .1))

# initialize empty matrices
mat_cor <- mat_pval <- mat_signif <- matrix(NA,
nrow = ncol(metabolites),
ncol = ncol(echocardiographic_parameters),
dimnames = list(colnames(metabolites),
colnames(echocardiographic_parameters)))

# for each pair, compute one cell of the matrice
for(metab in colnames(metabolites)){
for(param in colnames(echocardiographic_parameters)){

# correlation coefficient
mat_cor[metab, param] <- cor(metabolites[[metab]],
echocardiographic_parameters[[param]],
method = "spearman")

# pvalue
pval <- cor.test(metabolites[[metab]],
echocardiographic_parameters[[param]],
method = "spearman")[["p.value"]]

# decide nb of stars
if(pval >= 0.05){
stars <- ""
} else if(pval >= 0.01){
stars <- "*"
} else if(pval >= 1e-3){
stars <- "**"
} else{
stars <- "***"
}

# save pvalue and stars in the corresponding matrices
mat_pval[metab, param] <- pval
mat_signif[metab, param] <- stars
}
}

# check that we could have directly computed all the coefficients without a loop
all.equal(
mat_cor,
cor(metabolites,
echocardiographic_parameters,
method = "spearman")
)
#> [1] TRUE

pheatmap(mat_cor,
display_numbers = mat_signif,
fontsize_number = 20)

Created on 2023-11-06 with reprex v2.0.2

Hi, sorry for the delay.

yes, congratulation,
this is what i want to obtain, I'll try to reproduce your method using matrices like you.
thank you so much.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.