# 1. 导入所需的库。
library(vegan)
library(tidyverse)
library(ggalt)
library(car)
library(ggforce)
library(ggpubr)
library(patchwork)
# 2. 定义所需的函数。
pairwise.adonis1 <- function(x, factors, p.adjust.m) {
# 将输入转换为矩阵
x = as.matrix(x)
co = as.matrix(combn(unique(factors), 2))
# 初始化输出变量
pairs <- F.Model <- R2 <- p.value <- c()
# 对factors中的每一对执行adonis分析
for (elem in 1:ncol(co)) {
ad = adonis(x[factors %in% c(as.character(co[1, elem]), as.character(co[2, elem])),
factors %in% c(as.character(co[1, elem]), as.character(co[2, elem]))] ~
factors[factors %in% c(as.character(co[1, elem]), as.character(co[2, elem]))], permutations = 999)
pairs <- c(pairs, paste(co[1, elem], 'vs', co[2, elem]))
F.Model <- c(F.Model, ad$aov.tab[1, 4])
R2 <- c(R2, ad$aov.tab[1, 5])
p.value <- c(p.value, ad$aov.tab[1, 6])
}
# p值调整
p.adjusted = p.adjust(p.value, method = p.adjust.m)
pairw.res = data.frame(pairs, F.Model, R2, p.value, p.adjusted)
return(pairw.res)
}
# 3. 读取和处理数据。
setwd("C:\\Users\\Administrator\\Desktop")
otu <- read.table("./otu_table.txt", row.names = 1, sep = "\t", header = TRUE) %>% as.data.frame()
map <- read.table("./metadata3-1.txt", sep = "\t", header = TRUE)
colnames(map)[1] <- "ID"
row.names(map) <- map$ID
idx <- rownames(map) %in% colnames(otu)
map1 <- map[idx,]
otu <- otu[, rownames(map1)]
# 4. 进行adonis分析并计算统计值。
bray_curtis <- vegan::vegdist(t(otu), method = "bray", na.rm = TRUE)
ado <- adonis(bray_curtis ~ map1$Group, permutations = 999, method = "bray")
R2_value <- round(as.data.frame(ado$aov.tab[5])[1, 1], 3)
p_v_value <- as.data.frame(ado$aov.tab[6])[1, 1]
title <- paste("adonis:R ", R2_value, " p: ", p_v_value, sep = "")
# 5. 绘制PCoA图。
pcoa <- cmdscale(bray_curtis, k = 2, eig = TRUE)
points <- as.data.frame(pcoa$points) %>% dplyr::rename(x = "V1", y = "V2")
eig <- pcoa$eig
points <- cbind(points, map1[match(rownames(points), map1$ID),])
n <- 0.85
colors <- c("B73_Week4"="#00BFFF","B73_Week6"="#00BFFF","B73_Week8"="#00BFFF","B73_Week10"="#00BFFF","Mo17_Week4"="#FF4500","Mo17_Week6"="#FF4500","Mo17_Week8"="#FF4500","Mo17_Week10"="#FF4500")
# 定义形状
shapes <- c("B73_Week4"=24, "B73_Week6"=22, "B73_Week8"=21, "B73_Week10"=23,
"Mo17_Week4"=24, "Mo17_Week6"=22, "Mo17_Week8"=21, "Mo17_Week10"=23)
# 在ggplot中使用这些形状
p1 <- ggplot(points, aes(x = x, y = y, fill = Group, shape = Group)) +
geom_point(alpha = .7, size = 5) +
scale_shape_manual(values = shapes) + # 使用自定义形状
scale_fill_manual(values = colors) +
labs(x = paste("PCoA 1 (", format(100 * eig[1] / sum(eig), digits = 4), "%)", sep = ""),
y = paste("PCoA 2 (", format(100 * eig[2] / sum(eig), digits = 4), "%)", sep = ""), title = title) +
geom_mark_ellipse(aes(fill = Group, label = Group), alpha = 0.1, color = "grey", linetype = 3) +
theme_bw() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.text = element_text(color = "black", size = 9))
# 显示绘制的图
p1
# 6. 输出pairwise adonis结果。
pair_bray_adonis <- pairwise.adonis1(bray_curtis, map1$Group, p.adjust.m = "bonferroni")
# 存储为文本文件
write.table(as.data.frame(pair_bray_adonis), "table.txt", sep = "\t", quote = FALSE, row.names = FALSE)
tab2 <- ggtexttable(pair_bray_adonis, rows = NULL)
p2 <- tab2
p2
# 使用ggsave保存PCoA图为PNG格式
ggsave(filename = "PCoA_plot.png", plot = p1, width = 12, height = 10, units = "in", dpi = 300)
标签:rename,dplyr,PCoA,ggforce,第二,library,ggpubr From: https://www.cnblogs.com/wzbzk/p/17824158.html