写在前面
需求是对瘤胃宏基因组结果鉴定到的差异菌株与表观指标、瘤胃代谢组、血清代谢组、牛奶代谢组中有差异的部分进行关联分析,效果图如下:
数据准备
逗号分隔的csv格式文件,两个表格,一个是每个样本对应的表观指标数据,另一个是每个样本对应的菌群丰度,我这里用的是genus水平
- 需要关联的表观数据
rumen.csv
- 不同样本的菌群丰度
genus.csv
R包linkET可视化
- 装包
install.pakages("linkET")
library(linkET)
如果报错R版本有问题装不上(我的4.3.1版本R出现了这个报错)请尝试:
install.packages("devtools")
devtools::install_github("Hy4m/linkET", force = TRUE)
packageVersion("linkET")
- 读取数据
library(ggplot2)
rumen <- read.csv("rumen.csv",sep=",",row.name=1,stringsAsFactors = FALSE,check.names = FALSE)
genus <- read.csv("genus.csv",sep=",",row.name=1,stringsAsFactors = FALSE,check.names = FALSE)
#如果报错row.names重复错误请检查数据格式是否为csv
rumen.csv
组内相关系数
matrix_data(list(rumen = rumen)) %>%
as_md_tbl()
correlate(rumen) %>%
as_matrix_data()
correlate(rumen) %>%
as_md_tbl()
correlate(rumen) %>%
as_md_tbl() %>%
qcorrplot() +
geom_square()
#如果对“%>%”功能报错,装具有此功能的包即可,比如dplyr
library(vegan)
correlate(rumen, genus, method = "spearman") %>%
qcorrplot() +
geom_square() +
geom_mark(sep = '\n',size = 3, sig_level = c(0.05, 0.01, 0.001),
sig_thres = 0.05, color = 'white') + #添加显著性和相关性值
scale_fill_gradientn(colours = RColorBrewer::brewer.pal(11, "RdBu"))
- 两个表格进行关联生成相关性矩阵图,带显著性标记
library(vegan)
correlate(rumen, genus, method = "spearman") %>%
qcorrplot() +
geom_square() +
geom_mark(sep = '\n',size = 3, sig_level = c(0.05, 0.01, 0.001),
sig_thres = 0.05, color = 'white') + #添加显著性和相关性值
scale_fill_gradientn(colours = RColorBrewer::brewer.pal(11, "RdBu"))
- 加工可视化
library(dplyr)
mantel <- mantel_test(rumen, genus,
spec_select = list(Milk_yeild=1,Milk_fat=2,Urea_Nitrogen=3,Butyric_acid=4,Valeric_acid=5,BUN=6,
T_AOC=7,SOD=8,MDA=9,IgA=10,IgG=11))%>%
mutate(rd = cut(r, breaks = c(-Inf, 0.5, Inf),
labels = c("< 0.5", ">= 0.5")),
pd = cut(p, breaks = c(-Inf, 0.01, 0.05, Inf),
labels = c("< 0.01", "0.01 - 0.05", ">= 0.05")))
qcorrplot(correlate(genus), type = "lower", diag = FALSE) +
geom_square() +geom_mark(sep = '\n',size = 1.8, sig_level = c(0.05, 0.01, 0.001),
sig_thres = 0.05,color="white") +
geom_couple(aes(colour = pd, size = rd),
data = mantel,
curvature = nice_curvature()) +
scale_fill_gradientn(colours = RColorBrewer::brewer.pal(11, "RdBu")) +
scale_size_manual(values = c(0.5, 1, 2)) +
scale_colour_manual(values = color_pal(3)) +
guides(size = guide_legend(title = "Mantel's r",
override.aes = list(color = "black"),
order = 2),
colour = guide_legend(title = "Mantel's p",
override.aes = list(size = 3),
order = 1),
fill = guide_colorbar(title = "Pearson's r", order = 3))
- 不显著的灰色连接线部分也可以去掉让画面更干净。其余细节去AI加工即可。