缘由
统计是生信的基石之一
碎碎念
要爱一个具体的人,不要爱一个抽象的人; 要爱生活,不要爱生活的意义。 ——《卡拉马佐夫兄弟》
Ref
How to Add P-Values onto Horizontal GGPLOTS - Datanovia
解读
1. 基础图表准备与统计检验
数据准备与基础图表
library(ggpubr)
library(rstatix)
# 数据准备
df <- ToothGrowth
df$dose <- as.factor(df$dose)
head(df, 3)
# 创建基础图表
bxp <- ggboxplot(df, x = "dose", y = "len", fill = "dose",
palette = c("#00AFBB", "#E7B800", "#FC4E07"))
# 创建条形图
bp <- ggbarplot(df, x = "dose", y = "len", add = "mean_sd", fill = "dose",
palette = c("#00AFBB", "#E7B800", "#FC4E07"))
# 统计检验
stat.test <- df %>% t_test(len ~ dose)
stat.test2. 垂直图表的p值标注
箱线图与条形图的标准标注
# 箱线图p值标注
stat.test <- stat.test %>% add_xy_position(x = "dose")
bxp + stat_pvalue_manual(stat.test, label = "p.adj.signif", tip.length = 0.01)
# 条形图p值标注(基于均值±标准差计算位置)
stat.test <- stat.test %>% add_xy_position(fun = "mean_sd", x = "dose")
bp + stat_pvalue_manual(stat.test, label = "p.adj.signif", tip.length = 0.01)3. 水平图表的p值标注
基础水平图表转换
# 水平箱线图
stat.test <- stat.test %>% add_xy_position(x = "dose")
bxp +
stat_pvalue_manual(
stat.test, label = "p.adj.signif", tip.length = 0.01,
coord.flip = TRUE # 关键参数:适应坐标翻转
) +
coord_flip() # 坐标轴翻转
# 水平条形图
stat.test <- stat.test %>% add_xy_position(fun = "mean_sd", x = "dose")
bp + stat_pvalue_manual(
stat.test, label = "p.adj.signif", tip.length = 0.01,
coord.flip = TRUE
) +
coord_flip()4. 水平图表的高级定制
调整标签方向和位置
bxp +
stat_pvalue_manual(
stat.test, label = "p.adj", tip.length = 0.01,
coord.flip = TRUE,
angle = 0, # 标签角度(0度水平)
hjust = 0, # 水平对齐
vjust = c(0, 1.2, 0) # 垂直对齐(分别调整三个标签)
) +
scale_y_continuous(expand = expansion(mult = c(0.05, 0.3))) +
coord_flip()5. 基于参考组的多重比较
设定参考组的统计检验
# 以0.5剂量为参考组进行比较
stat.test <- df %>% t_test(len ~ dose, ref.group = "0.5")
stat.test垂直图表中的参考组比较
# 垂直箱线图
stat.test <- stat.test %>% add_xy_position(x = "dose")
bxp +
stat_pvalue_manual(stat.test, label = "p.adj", tip.length = 0.01) +
scale_y_continuous(expand = expansion(mult = c(0.05, 0.1)))
# 垂直条形图
stat.test <- stat.test %>% add_xy_position(fun = "mean_sd", x = "dose")
bp +
stat_pvalue_manual(stat.test, label = "p.adj.signif", tip.length = 0.01) +
scale_y_continuous(expand = expansion(mult = c(0.05, 0.1)))水平图表中的参考组比较
# 水平箱线图
stat.test <- stat.test %>% add_xy_position(x = "dose")
bxp +
stat_pvalue_manual(
stat.test, label = "p.adj", tip.length = 0.01,
coord.flip = TRUE
) +
scale_y_continuous(expand = expansion(mult = c(0.05, 0.1))) +
coord_flip()
# 水平条形图
stat.test <- stat.test %>% add_xy_position(fun = "mean_sd", x = "dose")
bp +
stat_pvalue_manual(
stat.test, label = "p.adj", tip.length = 0.01,
coord.flip = TRUE
) +
scale_y_continuous(expand = expansion(mult = c(0.05, 0.1))) +
coord_flip()6. 高级定位技巧
在group2位置显示p值
stat.test <- stat.test %>% add_xy_position(x = "dose")
bxp +
stat_pvalue_manual(
stat.test, label = "p = {p.adj}", tip.length = 0.01,
coord.flip = TRUE,
x = "group2", # 在group2位置显示
hjust = 0.4 # 水平居中
) +
scale_y_continuous(expand = expansion(mult = c(0.05, 0.2))) +
coord_flip()垂直显示p值标签
stat.test <- stat.test %>% add_xy_position(x = "dose")
bxp +
stat_pvalue_manual(
stat.test, label = "p = {p.adj}", tip.length = 0.01,
coord.flip = TRUE,
x = "group2",
angle = 90 # 标签垂直显示
) +
scale_y_continuous(expand = expansion(mult = c(0.05, 0.1))) +
scale_x_discrete(expand = expansion(mult = c(0.05, 0.3))) + # x轴扩展
coord_flip()关键技术要点总结
1. 水平图表核心参数
coord.flip = TRUE # 适应坐标翻转
coord_flip() # 实际翻转坐标轴2. 位置计算策略
- 箱线图:
add_xy_position(x = "dose") - 条形图:
add_xy_position(fun = "mean_sd", x = "dose") - 参考组比较:
t_test(len ~ dose, ref.group = "0.5")
3. 标签格式定制
label = "p.adj.signif" # 显著性星号
label = "p.adj" # 具体p值
label = "p = {p.adj}" # 自定义格式
label = "{p.adj}{p.adj.signif}" # 组合显示4. 布局精细调整
# 标签方向
angle = 0 # 水平
angle = 90 # 垂直
# 对齐方式
hjust = 0 # 左对齐
hjust = 0.5 # 居中
hjust = 1 # 右对齐
# 垂直位置
vjust = 0 # 底部
vjust = 0.5 # 居中
vjust = 1 # 顶部5. 显示空间管理
# y轴扩展(垂直图表)
scale_y_continuous(expand = expansion(mult = c(0.05, 0.1)))
# x轴扩展(水平图表)
scale_x_discrete(expand = expansion(mult = c(0.05, 0.3)))