How to clarify the correlation between two datasets (corr.test and corrplot)

Hi. I am Newbie in R. I am facing the problem of corr.test analysis. Actually, I would like to clarify the correlation between two different vectors ( Bacteria types versus environment factors).

Then, I would like to plot a graph like this:

There are two dataset for my analysis, here's the link:
(1) my_data = https://drive.google.com/file/d/1ieHhMf1SgWZL75RBDc6Gza4rHjkKAIux/view?usp=sharing
(2) bacteria = https://drive.google.com/file/d/1T_o5yntbSAdNdo7be5Ki1dpmhsQBD5Ic/view?usp=sharing

First, please forgive me that I copy and paste the code instead of using "reprex", so sorry as R always appear such kind of message: This reprex appears to crash R....

I follow the code that reply by former professional in R studio community and try to run the code as below. However, at the end, it appears this kind of error message as below:
Can't subset columns that don't exist. x Column rowname doesn't exist.
Run rlang::last_error() to see where the error occurred.

So, I think I have made some mistake in this corr.test and graph plotting, can anyone help me to solve this problem or discuss with me ? Thank you very much. :heart_eyes: :heart_eyes: :heart_eyes:

library(tidyverse)
library(corrplot)
library(corrr)

my_data <- read.csv(file.choose())
my_data

bacteria <- read.csv(file.choose())
bacteria

row.names(bacteria) <- LETTERS[1:10]
bacteria
has_rownames(bacteria)

row.names(my_data) <- LETTERS[1:10]
my_data
has_rownames(my_data)

library(RColorBrewer)
df <- bind_cols(bacteria[, -1],my_data[, -1])

res1 <- cor.mtest(df, conf.level = 0.95)
res2 <- res1$p %>%
as.data.frame() %>%
rownames<-(colnames(df)) %>%
colnames<-(colnames(df)) %>%
rownames_to_column("rowname") %>%
gather("var", "p", -rowname)
head(res2)

corrr::correlate(bind_cols(my_data[, -1], bacteria[, -1])) %>%
gather("var", "value", -rowname) %>%
left_join(res2) %>%
mutate(value = ifelse(p < 0.05, NA_integer_, value)) %>%
select(-p) %>%
spread(var, value) %>%
filter(rowname %in% colnames(my_data)) %>%
select(one_of(c("rowname", colnames(bacteria)[-1]))) %>%
as.data.frame() %>%
column_to_rownames("rowname") %>%
as.matrix() %>%
corrplot::corrplot(is.corr = FALSE, na.label.col = "white", col = brewer.pal (n=10, name= "PuOr"), tl.col = "black", tl.srt = 45)

As result, it appears this kind of error message as below:
Can't subset columns that don't exist. x Column rowname doesn't exist.
Run rlang::last_error() to see where the error occurred.

This works for me but CHECK !

# library(tidyverse)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tibble)
library(tidyr)
library(magrittr)
#> 
#> Attaching package: 'magrittr'
#> The following object is masked from 'package:tidyr':
#> 
#>     extract

library(corrplot)
#> Warning: package 'corrplot' was built under R version 4.1.1
#> corrplot 0.90 loaded
library(corrr)
#> Warning: package 'corrr' was built under R version 4.1.1

setwd("D:/data/R/RStudio_Community")
my_data <- read.csv("Correlation-matrix-parameter-v1.csv")
#my_data

bacteria <- read.csv("Correlation-matrix-phylum-v2.csv")
#bacteria

row.names(bacteria) <- LETTERS[1:10]
#bacteria
has_rownames(bacteria)
#> [1] TRUE

row.names(my_data) <- LETTERS[1:10]
#my_data
has_rownames(my_data)
#> [1] TRUE

library(RColorBrewer)
df <- bind_cols(bacteria[, -1],my_data[, -1])

res1 <- cor.mtest(df, conf.level = 0.95)
res2 <- res1$p %>% as.data.frame()  
rownames(res2) <- colnames(df)
colnames(res2) <- colnames(df)
res2 <- res2 %>%
rownames_to_column("rowname") %>%
gather("var", "p", -rowname)
#head(res2)

corrr::correlate(bind_cols(my_data[, -1], bacteria[, -1])) %>%
  pivot_longer(cols=-term,names_to="var",values_to = 'value') %>%
  rename(rowname = term) %>%
left_join(res2) %>%
mutate(value = ifelse(p < 0.05, NA_integer_, value)) %>%
select(-p) %>%
spread(var, value) %>%
filter(rowname %in% colnames(my_data)) %>%
select(one_of(c("rowname", colnames(bacteria)[-1]))) %>%
as.data.frame() %>%
column_to_rownames("rowname") %>%
as.matrix() %>%
corrplot::corrplot(is.corr = FALSE, na.label.col = "white", 
  col = brewer.pal (n=10, name= "PuOr"), tl.col = "black", tl.srt = 45)
#> 
#> Correlation method: 'pearson'
#> Missing treated using: 'pairwise.complete.obs'
#> Joining, by = c("rowname", "var")


Created on 2021-09-10 by the reprex package (v2.0.0)

1 Like

Hello @HanOostdijk ,

Thanks for your helping ! I really appreciate it !!

Looks useful!

Let's me try and check.

If any good news, i will tell you later. Thanks again.

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