R
复制代码
# install.packages("vegan")
library(vegan)
library(ggplot2)
library(ggpubr)
setwd("xxx")
# 使用read.table()函数读取数据
df <- read.table("xxx", header = TRUE, row.names = 1)
# 转置数据框
df <- t(df)
# 计算每个样品的香农多样性指数
shannon <- apply(df, 1, diversity)
# 创建一个数据框来存储样品名和对应的香农多样性指数
data <- data.frame(sample = names(shannon), shannon = shannon)
# 读取元数据
metadata <- read.table("mother_metadata.txt", header = TRUE, row.names = 1)
# 将元数据和样品数据合并到一个数据框中
data <- merge(data, metadata, by = "row.names", all.x = TRUE)
# 设置每个组的颜色
group_colors <- c("IC" = "#BD3C29", "RC" = "#0172B6", "OS" = "#78D3AC", "VS" = "#E18727")
# 首先设置比较的列表
compare_list <- list(
c("RC","VS"))
# 使用ggplot2创建箱线图
p <- ggplot(data, aes(x = group, y = shannon, fill = group, colour = group)) +
geom_boxplot(width = 0.5, alpha = 0.6, lwd = 1.15, outlier.shape = NA) + # 调整箱的大小
geom_jitter(width = 0.3, size = 3, alpha = 0.75) + # 添加散点
labs(y = "Shannon Diversity") +
scale_fill_manual(values = group_colors) + # 设置颜色
scale_color_manual(values = group_colors) +
theme_minimal() +
labs(x = NULL) +
theme(panel.border = element_rect(colour = "black", fill=NA, linewidth = 1.1)) +
stat_compare_means(
comparisons = compare_list,
method = "wilcox.test",
label = "p.signif",
hide.ns = TRUE)
# 添加检验结果
# 显示图形
#print(p)
# 保存图形为PDF
ggsave("barplot.pdf", p, height = 5, width = 5)