ggplot2绘制凋亡/坏死实验堆叠柱状图添加统计检验报错求助
问题
使用ggplot2绘制凋亡/坏死实验的堆叠柱状图,展示apop、nec、late三类数据的总和及各部分占比,尝试为每类数据添加配对t检验的显著性标记时,重复添加stat_compare_means层出现报错:
Error in `ggsignif::geom_signif()`: ! Problem while computing stat. i Error occurred in the 3rd layer. Caused by error in `compute_layer()`: ! `stat_signif()` requires the following missing aesthetics: y
数据
数据表格
| condition | rep | nec | late | apop |
|---|---|---|---|---|
| 37_colo_control | rep1 | 0.0209 | 0.0334 | 0.0405 |
| 37_colo_control | rep2 | 0.0013 | 0.0402 | 0.0541 |
| 37_colo_control | rep3 | 0.0076 | 0.0546 | 0.0707 |
| 42_colo_control | rep1 | 0.0147 | 0.0564 | 0.0616 |
| 42_colo_control | rep2 | 0.0233 | 0.0596 | 0.0762 |
| 42_colo_control | rep3 | 0.0176 | 0.0461 | 0.0507 |
| 37_colo_mmc | rep1 | 0.0121 | 0.0976 | 0.2370 |
| 37_colo_mmc | rep2 | 0.0086 | 0.1090 | 0.2410 |
| 37_colo_mmc | rep3 | 0.0076 | 0.1110 | 0.2890 |
| 42_colo_mmc | rep1 | 0.0087 | 0.1120 | 0.3020 |
| 42_colo_mmc | rep2 | 0.0122 | 0.1330 | 0.3270 |
| 42_colo_mmc | rep3 | 0.0087 | 0.1120 | 0.3020 |
数据框代码
the_data <- structure(list(condition = c("37_colo_control", "37_colo_control", "37_colo_control", "42_colo_control", "42_colo_control", "42_colo_control", "37_colo_mmc", "37_colo_mmc", "37_colo_mmc", "42_colo_mmc", "42_colo_mmc", "42_colo_mmc"), rep = c("rep1", "rep2", "rep3", "rep1", "rep2", "rep3", "rep1", "rep2", "rep3", "rep1", "rep2", "rep3"), nec = c(0.0209, 0.0013, 0.0076, 0.0147, 0.0233, 0.0176, 0.0121, 0.0086, 0.0076, 0.0087, 0.0122, 0.0087), late = c(0.0334, 0.0402, 0.0546, 0.0564, 0.0596, 0.0461, 0.0976, 0.109, 0.111, 0.112, 0.133, 0.112), apop = c(0.0405, 0.0541, 0.0707, 0.0616, 0.0762, 0.0507, 0.237, 0.241, 0.289, 0.302, 0.327, 0.302)), class = "data.frame", row.names = c(NA, 12L))
原绘图代码
library(ggpubr) library(ggprism) library(ggplot2) the_data <- read.csv(**[[see table for data]]**) factored_condition <- c("37_colo_control","37_colo_mmc","42_colo_control","42_colo_mmc") comparisons <- list(c(factored_condition[1],factored_condition[2]), c(factored_condition[1],factored_condition[3]), c(factored_condition[1],factored_condition[4]) ) the_data %>% group_by(condition) fig_bar <- ggplot(the_data, aes(x=factor(condition, levels=factored_condition)))+ geom_bar(aes(y=apop+nec+late),position=position_dodge(), stat="summary", fun="mean", fill = "grey65") + stat_compare_means(mapping = aes(y=apop), comparisons = comparisons, paired = TRUE, method = "t.test", label="p.signif", symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, Inf), symbols = c("****","***", "**", "*", " "))) + geom_bar(aes(y=nec+late),position=position_dodge(), stat="summary", fun="mean", fill = "grey45") + stat_compare_means(mapping = aes(y=late), comparisons = comparisons, paired = TRUE, method = "t.test", label="p.signif", symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, Inf), symbols = c("****","***", "**", "*", " "))) + geom_bar(aes(y=nec),position=position_dodge(), stat="summary", fun="mean", fill = "grey 15") + stat_compare_means(mapping = aes(y=nec), comparisons = comparisons, paired = TRUE, method = "t.test", label="p.signif", symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, Inf), symbols = c("****","***", "**", "*", " "))) + labs(y="Percent of Cells", x="", fill = "") + ggtitle("Colo205") + scale_y_continuous(expand=c(0,0),limits = c(0,1.0), labels = scales::percent) + scale_x_discrete(labels=x.names) + theme_prism() fig_bar
报错信息
Error in `ggsignif::geom_signif()`: ! Problem while computing stat. i Error occurred in the 3rd layer. Caused by error in `compute_layer()`: ! `stat_signif()` requires the following missing aesthetics: y Backtrace:
解决方案
错误原因
- 原代码用多次
geom_bar叠加模拟堆叠,这种方式不符合ggplot2的规范,且stat_compare_means的映射参数传递错误,导致无法识别y轴美学。 - 配对t检验需要确保重复样本一一对应,原宽格式数据结构不利于分组检验和绘图。
修正后代码
library(ggpubr) library(ggprism) library(ggplot2) library(tidyr) library(dplyr) # 转换为长格式数据,适配堆叠绘图和分组检验 long_data <- the_data %>% pivot_longer(cols = c(nec, late, apop), names_to = "type", values_to = "value") %>% mutate( condition = factor(condition, levels = c("37_colo_control","37_colo_mmc","42_colo_control","42_colo_mmc")), type = factor(type, levels = c("nec", "late", "apop")) ) # 定义比较组 comparisons <- list(c("37_colo_control","37_colo_mmc"), c("37_colo_control","42_colo_control"), c("37_colo_control","42_colo_mmc")) # 计算各类型数据的最大均值,用于设置显著性标记的y轴位置(避免重叠) mean_pos <- long_data %>% group_by(type) %>% summarise(max_mean = max(value), .groups = "drop") %>% mutate(y_pos = max_mean + c(0.05, 0.15, 0.25)) fig_bar <- ggplot(long_data, aes(x = condition, y = value, fill = type)) + # 绘制堆叠柱状图(展示均值) stat_summary(fun = mean, geom = "col", position = "stack") + # 为nec添加配对t检验显著性标记 stat_compare_means( data = filter(long_data, type == "nec"), comparisons = comparisons, paired = TRUE, method = "t.test", label = "p.signif", y.position = mean_pos$y_pos[mean_pos$type == "nec"], symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, Inf), symbols = c("****","***", "**", "*", " ")) ) + # 为late添加配对t检验显著性标记 stat_compare_means( data = filter(long_data, type == "late"), comparisons = comparisons, paired = TRUE, method = "t.test", label = "p.signif", y.position = mean_pos$y_pos[mean_pos$type == "late"], symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, Inf), symbols = c("****","***", "**", "*", " ")) ) + # 为apop添加配对t检验显著性标记 stat_compare_means( data = filter(long_data, type == "apop"), comparisons = comparisons, paired = TRUE, method = "t.test", label = "p.signif", y.position = mean_pos$y_pos[mean_pos$type == "apop"], symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, Inf), symbols = c("****","***", "**", "*", " ")) ) + # 绘图样式设置 labs(y="Percent of Cells", x="", fill = "Cell Type") + ggtitle("Colo205") + scale_y_continuous(expand = c(0,0), limits = c(0, 0.8), labels = scales::percent) + scale_fill_manual(values = c("nec" = "grey15", "late" = "grey45", "apop" = "grey65")) + theme_prism() fig_bar
说明
- 用
pivot_longer将宽格式转为长格式,符合ggplot2的绘图逻辑,便于分组处理数据。 - 通过
stat_summary直接绘制堆叠柱状图的均值,替代原多次geom_bar叠加的不规范方式。 - 针对每个细胞类型单独筛选数据传递给
stat_compare_means,并通过y.position设置错开的标记位置,避免重叠。 - 配对t检验的
paired=TRUE会自动基于rep列匹配重复样本,保证检验结果的准确性。
内容的提问来源于stack exchange,提问作者Ryan Fuller
相关产品推荐
相关产品推荐

