By Tingting, 25 September, 2025
Forums

shannon.simpson作图

# 原始数据
data <- data.frame(
  Type = c('genus', 'genus', 'genus'),
  Index = c('Richness', 'Shannon', 'Simpson'),
  disease_bark_1 = c(551, 5.8998, 0.9424),
  disease_bark_2 = c(550, 5.7923, 0.9317),
  disease_bark_3 = c(548, 5.5681, 0.9177),
  disease_soil_1 = c(115, 4.4081, 0.8709),
  disease_soil_2 = c(144, 5.6713, 0.9625),
  disease_soil_3 = c(143, 5.3292, 0.9461),
  healthy_bark_1 = c(578, 4.8785, 0.8434),
  healthy_bark_2 = c(582, 5.904, 0.9609),
  healthy_bark_3 = c(583, 5.5248, 0.9357),
  healthy_soil_1 = c(141, 5.3854, 0.9489),
  healthy_soil_2 = c(122, 5.3279, 0.9476),
  healthy_soil_3 = c(146, 4.9255, 0.9214)
)

# 加载必要的包
library(tidyr)
library(ggplot2)
library(RColorBrewer)

# 筛选出 genus 类型的数据
genus_data <- data[data$Type == "genus", ]

# 提取 Shannon 和 Simpson 数据
shannon_data <- genus_data[genus_data$Index == "Shannon", ]
simpson_data <- genus_data[genus_data$Index == "Simpson", ]

# 将数据转换为长格式
shannon_long <- gather(shannon_data, key = "Sample", value = "Shannon_Index", -Type, -Index)
simpson_long <- gather(simpson_data, key = "Sample", value = "Simpson_Index", -Type, -Index)

# 从 Sample 列中提取样本类型信息
shannon_long$Sample_Type <- gsub("_[0-9]", "", shannon_long$Sample)
simpson_long$Sample_Type <- gsub("_[0-9]", "", simpson_long$Sample)

# 获取 ColorBrewer 调色板中的颜色
colors <- brewer.pal(4, "Set2")
names(colors) <- c("disease_bark", "disease_soil", "healthy_bark", "healthy_soil")

# 计算每个组的最大值,用于放置显著性标注的位置
shannon_max <- aggregate(Shannon_Index ~ Sample_Type, data = shannon_long, FUN = max)
simpson_max <- aggregate(Simpson_Index ~ Sample_Type, data = simpson_long, FUN = max)

# 添加 ns 作为显著性标注
shannon_max$label <- "ns"
simpson_max$label <- "ns"

# 绘制 Shannon 指数箱线图,添加颜色、误差棒、显著性标志,设置白色背景
shannon_plot <- ggplot(shannon_long, aes(x = Sample_Type, y = Shannon_Index, fill = Sample_Type)) +
  geom_boxplot(outlier.shape = NA) +
  stat_boxplot(geom = 'errorbar', width = 0.2) +
  scale_fill_manual(values = colors) +
  geom_text(data = shannon_max, aes(x = Sample_Type, y = Shannon_Index + 0.1, label = label), size = 5) +
  labs(title = "Genus Shannon Index Boxplot",
       x = "Sample Type",
       y = "Shannon Index") +
  theme_bw() +
  theme(
    panel.grid.major = element_line(color = "gray90"),
    panel.grid.minor = element_line(color = "gray95"),
    legend.title = element_blank()
  )

print(shannon_plot)

# 绘制 Simpson 指数箱线图,添加颜色、误差棒、显著性标志,设置白色背景
simpson_plot <- ggplot(simpson_long, aes(x = Sample_Type, y = Simpson_Index, fill = Sample_Type)) +
  geom_boxplot(outlier.shape = NA) +
  stat_boxplot(geom = 'errorbar', width = 0.2) +
  scale_fill_manual(values = colors) +
  geom_text(data = simpson_max, aes(x = Sample_Type, y = Simpson_Index + 0.01, label = label), size = 5) +
  labs(title = "Genus Simpson Index Boxplot",
       x = "Sample Type",
       y = "Simpson Index") +
  theme_bw() +
  theme(
    panel.grid.major = element_line(color = "gray90"),
    panel.grid.minor = element_line(color = "gray95"),
    legend.title = element_blank()
  )

print(simpson_plot)