缘由

统计是生信的基石之一

碎碎念

要爱一个具体的人,不要爱一个抽象的人; 要爱生活,不要爱生活的意义。 ——《卡拉马佐夫兄弟》

Ref

GGPUBR: How to Add Adjusted P-values to a Multi-Panel GGPlot - Datanovia

基因表达差异分析完整教程

1. 数据加载与预处理

加载必要的R包

library(ggpubr)    # 数据可视化
library(rstatix)   # 统计检验
library(tidyverse) # 数据处理

创建模拟数据集

# Data preparation
df <- tibble::tribble(
	~sample_type, ~expression, ~cancer_type,   ~gene,
	"cancer",        25.8,       "Lung", "Gene1",
	"cancer",        25.5,      "Liver", "Gene1",
	"cancer",        22.4,      "Liver", "Gene1",
	"cancer",        21.2,       "Lung", "Gene1",
	"cancer",        24.5,      "Liver", "Gene1",
	"cancer",        27.3,      "Liver", "Gene1",
	"cancer",        30.9,      "Liver", "Gene1",
	"cancer",        17.6,     "Breast", "Gene1",
	"cancer",        19.7,       "Lung", "Gene1",
	"cancer",         9.7,     "Breast", "Gene1",
	"cancer",        15.2,     "Breast", "Gene2",
	"cancer",        26.4,      "Liver", "Gene2",
	"cancer",        25.8,       "Lung", "Gene2",
	"cancer",         9.7,     "Breast", "Gene2",
	"cancer",        21.2,       "Lung", "Gene2",
	"cancer",        24.5,      "Liver", "Gene2",
	"cancer",        14.5,     "Breast", "Gene2",
	"cancer",        19.7,       "Lung", "Gene2",
	"cancer",        25.2,       "Lung", "Gene2",
	"normal",        43.5,       "Lung", "Gene1",
	"normal",        76.5,      "Liver", "Gene1",
	"normal",        21.9,     "Breast", "Gene1",
	"normal",        69.9,      "Liver", "Gene1",
	"normal",       101.7,      "Liver", "Gene1",
	"normal",        80.1,      "Liver", "Gene1",
	"normal",        19.2,     "Breast", "Gene1",
	"normal",        49.5,       "Lung", "Gene1",
	"normal",        34.5,     "Breast", "Gene1",
	"normal",        51.9,       "Lung", "Gene1",
	"normal",        67.5,       "Lung", "Gene2",
	"normal",          30,     "Breast", "Gene2",
	"normal",        76.5,      "Liver", "Gene2",
	"normal",        88.5,      "Liver", "Gene2",
	"normal",        69.9,      "Liver", "Gene2",
	"normal",        49.5,       "Lung", "Gene2",
	"normal",        80.1,      "Liver", "Gene2",
	"normal",        79.2,      "Liver", "Gene2",
	"normal",        12.6,     "Breast", "Gene2",
	"normal",        97.5,      "Liver", "Gene2",
	"normal",        64.5,      "Liver", "Gene2"
)
 
  • 包含4个变量:
    • sample_type: 样本类型(癌症/正常)
    • expression: 基因表达量
    • cancer_type: 癌症类型
    • gene: 基因名称

2. 描述性统计分析

# Summary statistics
df %>%
    group_by(gene, cancer_type, sample_type) %>%
    get_summary_stats(expression, type = "common")
  • group_by(): 按基因、癌症类型和样本类型分组
  • get_summary_stats(): 计算常用统计量
  • 输出包括:样本数、均值、标准差、标准误、中位数等
  • 帮助了解数据的整体分布情况
  • 为后续统计检验提供基础信息

3. 统计检验与多重检验校正

执行t检验

stat.test <- df %>%
    group_by(cancer_type, gene) %>%           # 按癌症类型和基因分组
    t_test(expression ~ sample_type) %>%      # 执行t检验
    adjust_pvalue(method = "bonferroni") %>%  # P值校正
    add_significance()                        # 添加显著性标记
stat.test
  1. t_test()函数:

    • 公式expression ~ sample_type表示比较不同样本类型的表达差异
    • 自动执行两组间的t检验
  2. 多重检验校正:

    • adjust_pvalue(method = "bonferroni"): 使用Bonferroni法校正
    • 为什么需要校正? 当进行多次检验时,假阳性率会增加
    • 其他可选方法:BH(FDR)、holm、hochberg等
  3. 显著性标记:

    • add_significance(): 自动添加星号标记
    • * p < 0.05, ** p < 0.01, *** p < 0.001

4. 数据可视化

创建基础箱线图

bxp <- ggboxplot(
    df, x = "sample_type", y = "expression", 
    facet.by = c("gene", "cancer_type")  # 按基因和癌症类型分面
) +
    rotate_x_text(angle = 60)  # x轴文字旋转60度

添加P值位置信息

stat.test <- stat.test %>% add_xy_position(x = "sample_type")

多种P值显示方式

方式1:显示具体P值

bxp + stat_pvalue_manual(stat.test, label = "p.adj")

适用场景:需要精确P值的学术论文

方式2:显示显著性标记

bxp + stat_pvalue_manual(stat.test, label = "p.adj.signif")

适用场景:快速识别显著差异

方式3:隐藏不显著结果

bxp + stat_pvalue_manual(
    stat.test, label = "p.adj.signif",
    hide.ns = TRUE, tip.length = 0
)

参数说明:

  • hide.ns = TRUE: 隐藏不显著的结果
  • tip.length = 0: 设置指示线尖端长度为0

方式4:组合显示P值和显著性

bxp + stat_pvalue_manual(
    stat.test, label = "{p.adj}{p.adj.signif}",
    hide.ns = TRUE, tip.length = 0
)

显示效果:同时显示具体P值和星号标记