How to bind result of p value and r value in corrplot

Hi guys

I need help drawing a corrplot.

lee <- tibble::tribble(
    ~NAME, ~`Bacteria-1`, ~`Bacteria-2`, ~`Bacteria-3`, ~`Bacteria-4`, ~`Bacteria-5`, ~`Bacteria-6`, ~`Bacteria-7`, ~`Bacteria-8`, ~`Bacteria-9`, ~`Bacteria-10`, ~`Bacteria-11`, ~`Bacteria-12`, ~`Bacteria-13`, ~`Bacteria-14`,
  "CON-1",           41L,           13L,         3293L,           22L,           24L,           59L,           38L,          111L,           46L,             5L,           164L,            47L,            49L,            21L,
  "CON-2",           46L,           11L,         3099L,           33L,           27L,           66L,           42L,           98L,           37L,             8L,           170L,            50L,            60L,            26L,
  "CON-3",           58L,           12L,         3248L,           12L,           24L,           73L,           45L,           97L,           39L,            14L,           169L,            43L,            52L,            20L
  )


type or paste code here

kwon <- tibble::tribble(
    ~NAME,  ~pH, ~co2,  ~ch4,   ~o2,  ~n2o,  ~h2o,
  "CON-1", 47.2, 6.29, 65.19, 60.09, 25.64, 14.27,
  "CON-2", 48.2, 6.24, 67.76, 59.89, 26.77, 13.34,
  "CON-3", 44.6, 6.28, 65.29, 59.75, 26.24,    14
  )

Those are input data to drawing corrplot

and this is my code

library(corrplot)
library(tidyverse)
library(readxl)
library(Hmisc)


data1<-read.csv("raw_data/lee.csv",head=T)
data2<-read.csv("raw_data/kwon.csv", head=T)

cor_result<-matrix(NA, 6, 14)
pval_result<-matrix(NA, 6, 14)

%>% 

for(i in 2:ncol(data1)){
  
  for(j in 2:ncol(data2)){
    
    output<-  cor.test(data1[,i],data2[,j],  method = "pearson", use="na.or.complete")
    
    cor_result[j-1,i-1] <- output$estimate
    
    pval_result[j-1,i-1] <- output$p.value
    
  }}

colnames(cor_result) <- names(data1)[2:15]
rownames(cor_result) <- names(data2)[2:7]
colnames(pval_result) <- names(data1)[2:15]
rownames(pval_result) <- names(data2)[2:7]

cor_result[abs(cor_result) < 0.9] <- NA
pval_result[abs(cor_result) < 0.05] <-NA

corrplot(cor_result, method='circle',tl.cex=0.7, na.label=" ", tl.col="black", addCoef.col = 'white', number.cex = 1)
corrplot(pval_result, method='circle',tl.cex=0.7, na.label=" ", tl.col="black", addCoef.col = 'white', number.cex = 1)

I want to bind this pval_result and cor_result (together) to indicate corrplot as satisfied same times.
Some papers indicated that "pearson correlation coefficient (p<0.05, lrl>0.7)" were shown on the plot.

I also want to make plot like those conditions.

Please help me!

Perhaps this?

library(tidyverse)

var_list <- mtcars %>%
  select_if(is.numeric) %>%
  as.list() 

var_grid <- expand_grid(v1 = var_list, v2 = var_list) %>%
  mutate(
    name1 = names(v1), 
    name2 = names(v2), 
    ttest = map2(v1, v2, cor.test, method = "pearson", use="na.or.complete"),
    corr  = map_dbl(ttest, pluck, "estimate"),
    pval  = map_dbl(ttest, pluck, "p.value"),
    cond  = (pval < 0.05) & (corr > 0.7),
    condn = as.numeric(cond)
  )

print(var_grid)
#> # A tibble: 121 x 9
#>    v1           v2           name1 name2 ttest         corr     pval cond  condn
#>    <named list> <named list> <chr> <chr> <named lis>  <dbl>    <dbl> <lgl> <dbl>
#>  1 <dbl [32]>   <dbl [32]>   mpg   mpg   <htest>      1     0        TRUE      1
#>  2 <dbl [32]>   <dbl [32]>   mpg   cyl   <htest>     -0.852 6.11e-10 FALSE     0
#>  3 <dbl [32]>   <dbl [32]>   mpg   disp  <htest>     -0.848 9.38e-10 FALSE     0
#>  4 <dbl [32]>   <dbl [32]>   mpg   hp    <htest>     -0.776 1.79e- 7 FALSE     0
#>  5 <dbl [32]>   <dbl [32]>   mpg   drat  <htest>      0.681 1.78e- 5 FALSE     0
#>  6 <dbl [32]>   <dbl [32]>   mpg   wt    <htest>     -0.868 1.29e-10 FALSE     0
#>  7 <dbl [32]>   <dbl [32]>   mpg   qsec  <htest>      0.419 1.71e- 2 FALSE     0
#>  8 <dbl [32]>   <dbl [32]>   mpg   vs    <htest>      0.664 3.42e- 5 FALSE     0
#>  9 <dbl [32]>   <dbl [32]>   mpg   am    <htest>      0.600 2.85e- 4 FALSE     0
#> 10 <dbl [32]>   <dbl [32]>   mpg   gear  <htest>      0.480 5.40e- 3 FALSE     0
#> # ... with 111 more rows

palettes <- tibble(
  name = c("corr", "pval", "condn"),
  pal  = c("Blues", "Greens", "Oranges"),
  direct = c(1, -1, 1)
)

var_grid %>%
  pivot_longer(c(corr, pval, condn)) %>%
  left_join(palettes, by = "name") %>%
  # separate plot for each variable
  split(.$name) %>%
  map(
    ~ggplot(.) + 
      aes(name1, name2, fill = value) + 
      geom_tile() +
      ggtitle(unique(.$name)) + 
      scale_fill_distiller(palette = unique(.$pal), direction = unique(.$direct)) + 
      scale_y_discrete(limits = rev)
  ) %>%
  walk(print)

Created on 2022-01-26 by the reprex package (v2.0.1)

1 Like

Thank you very much for quick comment!
However, is it possible to make three heatmap into one heatmap?
I am looking for a plot that considers both p value and r value !
But i really appreciate your work !

You're welcome. That was my intent for "condn" which is the condition you specified for p and r.

Otherwise, I don't think it's possible to plot 3 variables with color scales in one heat map. There can only be one color per cell.

Exactly! so i want to solve this problem by drawing to corrplot matrix!
something like this figure

Hm. I don't understand. The corrplot matrix only shows correlations. How do you suggest to visualize three values (correlation coefficient, p-value, specified condition) simultaneously in each cell?

This topic was automatically closed 21 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.