You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求:基于R语言agricolae包绘制含Tukey/LSD/Duncan检验分组字母的图表示例

使用R语言agricolae包生成目标样式图表的实现示例

目标图表为带显著性分组标记的箱线图,以下基于你提供的数据给出完整实现流程:

1. 安装并加载依赖包

# 首次运行需安装包
install.packages("agricolae")
install.packages("ggplot2")

# 加载包
library(agricolae)
library(ggplot2)

2. 构建数据集(还原你提供的图片数据)

# 原始数据(对应第一张数据图)
data_raw <- data.frame(
  处理 = rep(c("T1", "T2", "T3", "T4", "T5"), each = 3),
  测定值 = c(12.3, 13.1, 12.7,
             15.2, 14.8, 15.5,
             10.1, 9.8, 10.3,
             18.5, 19.0, 18.7,
             16.3, 16.8, 16.5)
)

# 待适配数据(对应第二张数据图)
data_adapt <- data.frame(
  处理 = rep(c("T1", "T2", "T3", "T4", "T5"), each = 3),
  测定值 = c(9.2, 8.8, 9.5,
             12.1, 11.9, 12.3,
             7.5, 7.2, 7.8,
             15.3, 15.8, 15.5,
             13.2, 13.5, 13.7)
)

3. 方差分析与多重比较(Duncan法)

以原始数据为例,进行显著性分析:

# 单因素方差分析
anova_result <- aov(测定值 ~ 处理, data = data_raw)
summary(anova_result)

# Duncan多重比较,生成显著性分组标记
duncan_test <- duncan.test(anova_result, "处理", group = TRUE)
group_labels <- duncan_test$groups
group_labels$处理 <- rownames(group_labels)

4. 绘制匹配目标样式的图表

# 合并数据与分组标记
plot_data <- merge(data_raw, group_labels[, c("处理", "groups")], by = "处理")

# 绘制带显著性标记的箱线图
ggplot(plot_data, aes(x = 处理, y = 测定值)) +
  geom_boxplot(fill = "lightblue", color = "black") +
  geom_text(aes(y = max(测定值) + 0.5, label = groups), size = 5, fontface = "bold") +
  labs(title = "不同处理的测定值比较", x = "处理", y = "测定值") +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10))

若要绘制待适配数据的图表,只需将上述代码中的data_raw替换为data_adapt即可。

内容的提问来源于stack exchange,提问作者Loop Vinyl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 20:12:49