如何在单柱状图中组合多Y变量并按处理组执行非配对t检验?
多变量时期差异t检验与2×2柱状图绘制方案
需求明确
要把CAT、SOD、GSH、MDA四个指标整合为2×2布局的柱状图:
- X轴为
Treatments分组,每个处理组对应Pre-challenge和Post-challenge两个时期的柱形 - 针对每个指标,按处理组分别对两个时期做非配对t检验,并用显著性标记(字母或星号)标注差异
数据集
CAT SOD GSH MDA Treatments Period 2.06 5.18 8.12 2.5 CFP0 Pre-challenge 2.02 5.14 8.18 2.7 CFP0 Pre-challenge 2.68 5.11 8.01 3.12 CFP1 Pre-challenge 2.66 6.15 8.21 3.53 CFP1 Pre-challenge 2.7 5.84 8 4.3 CFP2 Pre-challenge 2.76 5.86 8.2 4.27 CFP2 Pre-challenge 2.92 6.35 7.14 4.79 CFP3 Pre-challenge 2.72 6.35 7.1 4.83 CFP3 Pre-challenge 3.01 7.19 6.36 5 CFP4 Pre-challenge 2.81 7.17 6.42 5.02 CFP4 Pre-challenge 3 8.29 5.13 6.5 CFP5 Pre-challenge 3.2 9.87 5.17 6.58 CFP5 Pre-challenge 5.58 7.12 8.52 7.6 CFP0 Post-challenge 5.4 7.14 8.58 9.2 CFP0 Post-challenge 3.93 4.2 7.4 4.04 CFP1 Post-challenge 3.77 4.5 7.8 4.03 CFP1 Post-challenge 3.81 4.6 7.1 4.2 CFP2 Post-challenge 3.95 4.26 7.86 4.7 CFP2 Post-challenge 3.81 4.68 7.2 4.6 CFP3 Post-challenge 4.2 4.58 7.41 4.7 CFP3 Post-challenge 4.6 6.18 8.3 7.6 CFP4 Post-challenge 4.9 6.09 8.4 7.9 CFP4 Post-challenge 4.9 6.2 8.2 8.96 CFP5 Post-challenge 4.8 6.16 7.9 9.02 CFP5 Post-challenge
现有代码(单变量绘图示例)
library(readxl) library(ggpubr) immun <- read_excel("P_II.xlsx", sheet = "imm_data") summary(immun) immun$Period <- as.factor(immun$Period) immun$Treatments <- as.factor(immun$Treatments) # 单变量柱状图 imm <- ggbarplot(immun, x = "Treatments", y = c("Lysozyme"), combine = TRUE, color = "Period", width = 0.5, add = "mean_se", error.plot = "errorbar", palette = c("#585858","#c6c6c6"), fill = "Period", position = position_dodge(0.6)) ggpar(imm, ylim = c(120, 190), ylab = "Lysozyme (pg/mL)", title = "Lysozyme")
完整解决方案
1. 批量执行非配对t检验
用dplyr按处理组分批对每个指标做t检验,先安装加载dplyr:
install.packages("dplyr") library(dplyr) # 定义检验函数:输入数据和指标名,返回各处理组的p值 run_tests <- function(data, var_name) { data %>% group_by(Treatments) %>% summarise( p_value = t.test(!!sym(var_name) ~ Period, paired = FALSE)$p.value, .groups = "drop" ) %>% mutate(variable = var_name) } # 批量处理四个指标 test_results <- bind_rows( run_tests(immun, "CAT"), run_tests(immun, "SOD"), run_tests(immun, "GSH"), run_tests(immun, "MDA") ) # 查看检验结果 print(test_results)
2. 生成显著性标记
可以选择星号或字母标注,这里两种方式都提供:
# 安装multcompView包(用于字母标注) install.packages("multcompView") library(multcompView) # 给检验结果添加显著性标记 test_results <- test_results %>% mutate( # 星号标记 sig_stars = case_when( p_value < 0.001 ~ "***", p_value < 0.01 ~ "**", p_value < 0.05 ~ "*", TRUE ~ "ns" ), # 字母标记(两个组比较,显著则分a/b,不显著统一a) sig_letters = ifelse(p_value < 0.05, "a,b", "a") )
3. 绘制2×2布局的多变量柱状图
先把数据转成长格式,再用ggpubr批量绘图并添加标注:
install.packages("tidyr") library(tidyr) # 转长格式,方便批量绘图 immun_long <- immun %>% pivot_longer(cols = c(CAT, SOD, GSH, MDA), names_to = "Variable", values_to = "Value") # 绘制2×2组合柱状图 combined_plot <- ggbarplot(immun_long, x = "Treatments", y = "Value", facet.by = "Variable", ncol = 2, # 2×2布局 color = "Period", width = 0.5, add = "mean_se", error.plot = "errorbar", palette = c("#585858","#c6c6c6"), fill = "Period", position = position_dodge(0.6)) # 添加显著性星号标注(每个处理组的两个柱形上方) combined_plot <- combined_plot + geom_text(data = test_results, aes(x = Treatments, y = max(immun_long$Value)*1.1, label = sig_stars), position = position_dodge(0.6), size = 4) # 调整图表样式 ggpar(combined_plot, ylab = "Indicator Value", title = "Biochemical Indicators: Pre vs Post Challenge", legend.title = "Period")
关键提示
- 非配对t检验必须设置
paired=FALSE,确保和实验设计匹配 - 长格式数据是批量绘制多变量图的核心,
pivot_longer能快速完成宽转长 - 若想用字母标注每个时期的差异,可以把
sig_letters拆分成两个值,分别对应Pre和Post时期的柱形上方
内容的提问来源于stack exchange,提问作者Muhammad Bello
相关产品推荐
相关产品推荐

