R可视化——Mantel test分析及可视化

    此前我们已经讲过了如何使用R语言计算两列数据相关性的分析方法,今天,我们来看一种检验两个矩阵相关关系的方法——Mantel test方法,这种分析方法可用在植物微生物群落与生态环境之间相关性的检验以及人体肠道微生物群落与人体疾病相关性检验等领域。话不多说,直接迈入正文!

加载包

1)设置工作目录

rm(list=ls())#clear Global Environment
setwd('D:\\桌面\\mantel test分析')#设置工作路径

2)安装、加载R包

#安装包
# install.packages("ggplot2")
# install.packages("vegan")
# install.packages("dplyr")
# install.packages("devtools")
# devtools::install_github("houyunhuang/ggcor")

#加载包
library(vegan)
library(dplyr)
library(ggcor)
library(ggplot2)

加载数据

#OTU表格
df <- read.table("otu.txt",sep="\t",header = T,row.names = 1,check.names = F)
df <-data.frame(t(df))
#环境因子数据
env <- read.table("env.txt",sep="\t",header = T,row.names = 1,check.names = F)
head(df)
head(env)
image.png

image.png

环境因子的相关性分析及展示

quickcor(env, type = "lower",method = "spearman") +
  geom_square()+
  scale_fill_gradient2( high = 'orange', mid = 'white',low = 'navyblue')  #颜色设置
image.png

Mantel test分析

1)计算OTU与环境因子之间的mantel test的r值和p值

df_mantel <- mantel_test(df, env, mantel.fun = 'mantel',
                         spec.dist.method = 'bray', 
                         env.dist.method = 'euclidean',
                      spec.select = list(A = 1:4,
                                         B = c(6,8,9,10),
                                         C = c(5,7,11,12)))#将群落数据按组进行分开

2)定义标签

df_mantel <- df_mantel %>%
  mutate(df_r = cut(r, breaks = c(-Inf, 0.1, 0.2, 0.4, Inf),
                labels = c("< 0.1", "0.1 - 0.2", "0.2 - 0.4", ">= 0.4")),#定义Mantel的R值范围标签
       df_p = cut(p.value, breaks = c(-Inf, 0.01, 0.05, Inf),
                labels = c("< 0.01", "0.01 - 0.05", ">= 0.05")))#定义Mantel的P值范围标签
image.png

绘图

quickcor(env,method = "spearman", type = "upper", cor.test = T, cluster.type = "all") +#环境因子之间的相关性热图
  geom_square() +#相关性显示形式
  geom_mark(r = NA,sig.thres = 0.05, size = 3.5, colour = "black")+#显著性标签
  scale_fill_gradient2( high = 'red', mid = 'white',low = 'blue') + #颜色设置
  anno_link(df_mantel, aes(color = df_p,
                               size = df_r))+
  scale_size_manual(values = c(0.5, 1, 1.5, 2))+#连线粗细设置
  scale_color_manual(values = c("green","blue","orange"))+#线条颜色设置
  guides(fill = guide_colorbar(title = "correlation", order = 1),#图例相关设置
         size = guide_legend(title = "Mantel's r",order = 2),
         color = guide_legend(title = "Mantel's p", order = 3),
         linetype = "none")
image.png

参考:

1)https://zhuanlan.zhihu.com/p/507384776
2)https://zhuanlan.zhihu.com/p/110501058

源码及作图数据可在后台回复“mantel test”获取!!!

你可能感兴趣的:(R可视化——Mantel test分析及可视化)