R语言:Mantel Test分析与绘图

Mantel Test

  • [1.什么是Mantel Test](#1.什么是Mantel Test)
  • [2. R语言代码1](#2. R语言代码1)
  • [3. R语言代码2](#3. R语言代码2)

1.什么是Mantel Test

Mantel test分析对两个矩阵相关关系进行检验。可以用在生态学上,用来检验群落距离矩阵(如 Bray-Curtis distance matrix)和环境变量距离矩阵(如 pH, 温度 或者地理位置的差异矩阵)之间的相关性(Spearman、Pearson)。Mantel test的相关性系数越大,p值越小,则说明环境因子对微生物群落的影响越大。同时,mantel test的偏分析(partial Mantel test等)可排除环境因子之间自相关的干扰。

Mantel test分析结果:

文献参考: Structure and function of the global ocean microbiome

2. R语言代码1

这里我使用了R内置的两个数据进行分析,并对图像进行了美化和调整。

在这里我使用的是linkET包,当然你也可以使用ggcor包,但是我不能安装这个包,所以就没有使用。

r 复制代码
#热图+网络图展示mantel test相关性
# 加载包
# devtools::install_github("Hy4m/linkET", force = TRUE)
library(linkET)
library(tidyverse)
library(RColorBrewer)

data("varechem", package = "vegan")
data("varespec", package = "vegan")
head(varespec[,1:6])#rownames is samples
head(varechem[,1:6])#rownames is samples
dim(varespec)#24,44
dim(varechem)#24,14

mantel <- mantel_test(varespec, varechem,
                      spec_select = list(Spec01 = 1:7,
                                         Spec02 = 8:18,
                                         Spec03 = 19:37,
                                         Spec04 = 38:44)) %>%
  dplyr::mutate(rd = cut(r, breaks = c(-Inf, 0.2, 0.4, Inf),
                         labels = c("< 0.2", "0.2 - 0.4", ">= 0.4")),
                pd = cut(p, breaks = c(-Inf, 0.01, 0.05, Inf),
                         labels = c("< 0.01", "0.01 - 0.05", ">= 0.05")));
mantel

correlate(varechem) %>%
  qcorrplot(type = "lower", diag = T) +
  geom_square() +
  geom_couple(aes(colour = pd, size = rd), data = mantel, curvature = 0.1) +
  geom_mark(sep = '\n',size = 4, sig_level = c(0.05, 0.01, 0.001),
            sig_thres = 0.05, color = 'black',
            ) +
  scale_fill_gradientn(colours = RColorBrewer::brewer.pal(9, "RdBu")) +
  scale_size_manual(values = c(0.5, 1, 2)) +
  scale_colour_manual(values = color_pal(3)) +
  labs(fill = "Pearson's correlation",
       size = "Mantel's r value",
       colour = "Mantel's p value")+
  theme(
    text = element_text(size = 14, family = "serif"),
    plot.title = element_text(size = 14, colour = "black", hjust = 0.5),
    legend.title = element_text(color = "black", size = 14),
    legend.text = element_text(color = "black", size = 14),
    axis.text.y = element_text(size = 14, color = "black", vjust = 0.5, hjust = 1, angle = 0),
    axis.text.x = element_text(size = 14, color = "black", vjust = 0.5, hjust = 0.5, angle = 0)
  )
  

结果展示:

3. R语言代码2

r 复制代码
rm(list=ls())#好习惯,确保有干净的 R 环境
# setwd("C:/Users/Desktop/take")
library(linkET)
library(ggplot2)
library(ggtext)
library(dplyr)
library(RColorBrewer)
library(cols4all)
library(tidyverse)

data("varechem", package = "vegan")
data("varespec", package = "vegan")

#计算环境因子相关性系数:
cor2 <- correlate(varechem)
corr2 <- cor2 %>% as_md_tbl()
write.csv(corr2, file = "pearson_correlate(env&env).csv", row.names = TRUE)

head(corr2)
#mantel test:
mantel <- mantel_test(varespec, varechem,
                      mantel_fun = 'mantel', #支持4种:"mantel"使用vegan::mantel();"mantel.randtest"使用ade4::mantel.randtest();"mantel.rtest"使用ade4::mantel.rtest();"mantel.partial"使用vegan::mantel.partial()
                      spec_select = list(spec01= 1:1,
                                         spec02=5:5,
                                         spec03 = 7:7
                      )) #这里分组为随机指定,具体实操需按自己的实际数据分组
head(mantel)
write.csv(mantel, file = "mantel_result(bio&env).csv", row.names = TRUE)

#对mantel的r和P值重新赋值(设置绘图标签):
mantel2 <- mantel %>%
  mutate(r = cut(r, breaks = c(-Inf, 0.25, 0.5, Inf),
                 labels = c("<0.25", "0.25-0.5", ">=0.5")),
         p = cut(p, breaks = c(-Inf, 0.001, 0.01, 0.05, Inf),
                 labels = c("<0.001", "0.001-0.01", "0.01-0.05", ">= 0.05")))
head(mantel2)
#首先,绘制相关性热图(和上文相同):


##############################
p4 <- qcorrplot(cor2,
                grid_col = "#00468BFF",
                "white","#42B540FF",
                grid_size = 0.2,
                type = "upper",
                diag = FALSE) +
  geom_square() +
  scale_fill_gradientn(colours = c("#00468BFF",
                                   "white","#42B540FF"),
                       limits = c(-1, 1))

# 打印出来看看

#添加显著性标签:
p5 <- p4 +
  geom_mark(size = 4,
            only_mark = T,
            sig_level = c(0.05, 0.01, 0.001),
            sig_thres = 0.05,
            colour = 'white')
p5
#在相关性热图上添加mantel连线:
p6 <- p5 +
  geom_couple(data = mantel2,
              aes(colour = p, size = r),
              curvature = nice_curvature())
p6
#继续美化连线:
p7 <- p6 +
  scale_size_manual(values = c(1, 2, 3)) + #连线粗细
  scale_colour_manual(values = c4a('brewer.set2',4)) + #连线配色
  #修改图例:
  guides(size = guide_legend(title = "Mantel's r",
                             override.aes = list(colour = "grey35"),
                             order = 2),
         colour = guide_legend(title = "Mantel's p",
                               override.aes = list(size = 5),
                               order = 1),
         fill = guide_colorbar(title = "Pearson's r", order = 3))+
  theme(
    text = element_text(size = 16, family = "serif"),
    plot.title = element_text(size = 16, colour = "black", hjust = 0.5),
    legend.title = element_text(color = "black", size = 16),
    legend.text = element_text(color = "black", size = 16),
    axis.text.y = element_text(size = 16, color = "black", vjust = 0.5, hjust = 1, angle = 0),
    axis.text.x = element_text(size = 16, color = "black", vjust = 0.5, hjust = 0.5, angle = 0)
  )

p7

结果展示:

相关推荐
啟明起鸣6 小时前
【C++ 性能提升技巧】C++ 的引用、值类型、构造函数、移动语义与 noexcept 特性,可扩容的容器
开发语言·c++
故以往之不谏6 小时前
函数--值传递
开发语言·数据结构·c++·算法·学习方法
卢锡荣6 小时前
Type-c OTG数据与充电如何进行交互使用应用讲解
c语言·开发语言·计算机外设·电脑·音视频
宏集科技工业物联网6 小时前
预防性维护与能源效率:SCADA 在工业运营中的关键作用
经验分享·scada·预测性维护·工业自动化·能耗管理
A懿轩A6 小时前
【Java 基础编程】Java 变量与八大基本数据类型详解:从声明到类型转换,零基础也能看懂
java·开发语言·python
2301_811232986 小时前
低延迟系统C++优化
开发语言·c++·算法
我能坚持多久6 小时前
D20—C语言文件操作详解:从基础到高级应用
c语言·开发语言
橘子师兄7 小时前
C++AI大模型接入SDK—ChatSDK封装
开发语言·c++·人工智能·后端
上天_去_做颗惺星 EVE_BLUE7 小时前
Docker高效使用指南:从基础到实战模板
开发语言·ubuntu·docker·容器·mac·虚拟环境