R:箱线图(第二版)

# 清除所有变量
rm(list = ls())

# 加载必要的包
library(ggplot2)
library(ggpubr)
library(dplyr)
library(multcompView)

# 设置工作目录
setwd("C:\\Users\\Administrator\\Desktop\\新建文件夹\\Flora difference")

# 读取数据
index <- read.table('1.txt', header = TRUE, row.names = 1)

# 进行ANOVA分析并进行Tukey多重比较
anova_result <- aov(Shannon ~ Group, data = index)
tukey_result <- TukeyHSD(anova_result)

# 使用 multcompLetters 函数处理 Tukey 测试结果
tukey_result <- TukeyHSD(anova_result)
comp_matrix <- tukey_result$Group[, "p adj"]
names(comp_matrix) <- rownames(tukey_result$Group)

tukey_letters <- multcompLetters(comp_matrix, compare="<", threshold=0.05)

# 创建标签数据框
group_labels <- data.frame(Group = names(tukey_letters$Letters), label = tukey_letters$Letters)

# 首先计算每个组的Shannon指数最大值并加上一定的偏移量
library(dplyr)
group_max <- index %>% 
  group_by(Group) %>% 
  summarise(MaxShannon = max(Shannon, na.rm = TRUE) * 1.01) # 计算每组的最大值并稍微提高以避免重叠

# 将计算出的最大值合并回group_labels数据框中
group_labels <- merge(group_labels, group_max, by.x = "Group", by.y = "Group")

# 绘制箱线图并美化,添加自动化标签
p <- ggboxplot(index, x = "Group", y = "Shannon", color = "Group", width = 0.8,add = "jitter",
               add.params = list(size = 3, alpha = 0.75)) +
  theme_minimal(base_size = 14) +
  labs(title = "", x = "", y = "Achromobacter") +
  theme(legend.position = "top",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        axis.line = element_line(colour = "black"),
        axis.text.x = element_text(size = 12, face = "plain", color = "black", angle = 45, hjust = 1),
        axis.text.y = element_text(size = 12, face = "plain", color = "black"),
        legend.title = element_blank(),
        axis.ticks = element_line(color = "black"),
        axis.ticks.length = unit(0.25, "cm"),
        legend.key.size = unit(0.8, "cm"),# 自定义图例项的大小
        legend.text = element_text(size = 10)) + # 自定义图例文本的文字大小+
  scale_color_manual(values = c("B73_DAS28" = "#9BC985", "Mo17_DAS28" = "#9BC985", "B73_DAS42" = "#F7D58B", "Mo17_DAS42" = "#F7D58B", "B73_DAS56" = "#B595BF", "Mo17_DAS56" = "#B595BF", "B73_DAS70" = "#797BB7", "Mo17_DAS70" = "#797BB7"),
                     labels = c("DAS28", "DAS42", "DAS56", "DAS70"),
                     breaks = c("B73_DAS28", "B73_DAS42", "B73_DAS56", "B73_DAS70")) +
  guides(color = guide_legend(override.aes = list(color = c("#9BC985", "#F7D58B", "#B595BF", "#797BB7")))) +
  geom_text(data = group_labels, aes(x = Group, y = MaxShannon, label = label), vjust = 0)  # 使用计算的最大值作为y坐标

# 计算B73和Mo17直线的y坐标
b73_y <- max(group_labels[group_labels$Group %in% c("B73_DAS28", "B73_DAS42", "B73_DAS56", "B73_DAS70"), ]$MaxShannon)
mo17_y <- max(group_labels[group_labels$Group %in% c("Mo17_DAS28", "Mo17_DAS42", "Mo17_DAS56", "Mo17_DAS70"), ]$MaxShannon)

# 为了美观,可能需要对y值进行小幅调整
adjustment <- 0.05 * (b73_y - min(index$Shannon))
b73_y <- b73_y + adjustment
mo17_y <- mo17_y + adjustment

# 绘制箱线图,添加B73和Mo17的直线及标签
p <- p + 
  annotate("segment", x = 0.5, xend = 4.5, y = b73_y, yend = b73_y, color = "#8FC9E2", size = 1.2) +  # 添加B73的红线
  annotate("text", x = 2.5, y = b73_y, label = "B73", vjust = -0.5, color = "#8FC9E2", size = 5) +   # 添加"B73"文本
  annotate("segment", x = 4.5, xend = 8.5, y = mo17_y, yend = mo17_y, color = "#ECC97F", size = 1.2) + # 添加Mo17的蓝线
  annotate("text", x = 6.5, y = mo17_y, label = "Mo17", vjust = -0.5, color = "#ECC97F", size = 5)    # 添加"Mo17"文本

# 显示图形
print(p)

# 重新保存图形,无警告
ggsave("boxplot.png", plot = p, width = 8, height = 8, dpi = 600, bg = "white")

 

posted @ 2024-03-08 09:19  王哲MGG_AI  阅读(27)  评论(0编辑  收藏  举报