使用 ggplot2 在箱线图中添加用于组比较的显着性标记

问题描述 投票:0回答:1

我有如下数据:

set.seed(123)
expr_data <- data.frame(
    cell_line_name = rep(c("CL1", "CL2"), each = 50),
    GSE_id = rep(c("GSE1", "GSE2"), each = 50),
    model = rep(c("M1", "M2"), each = 50),
    expr = rnorm(100),
    age_group = sample(c('A', 'B'), 100, replace = TRUE)
)
    cell_line_name GSE_id model        expr age_group
1              CL1   GSE1    M1 -0.34391723         B
2              CL1   GSE1    M1  0.09049665         A
3              CL1   GSE1    M1  1.59850877         A
4              CL1   GSE1    M1 -0.08856511         A
5              CL1   GSE1    M1  1.08079950         B
6              CL1   GSE1    M1  0.63075412         B
7              CL1   GSE1    M1 -0.11363990         A
8              CL1   GSE1    M1 -1.53290200         B
9              CL1   GSE1    M1 -0.52111732         A
10             CL1   GSE1    M1 -0.48987045         B
11             CL1   GSE1    M1  0.04715443         A

我如何绘制一个箱线图,其中 cell_line_name、GSE_id 和模型为 x 轴,expr 为 y 轴,并显示 A 组和 B 组之间的比较

ggplot(expr_data.gene, aes(x = paste0(cell_line_name, '_', GSE_id, '_', model), y = expr, color = age_group)) + 
    geom_boxplot() + 
    geom_point(position = position_jitterdodge(), alpha = 0.5)

我还为x值中的每个比较添加signif标签,我已经尝试过

ggsignif
ggpubr
,但不起作用。

ggplot(expr_data.gene, aes(x = paste0(cell_line_name, '_', GSE_id, '_', model), y = expr, color = age_group)) + 
    geom_boxplot() + 
    geom_point(position = position_jitterdodge(), alpha = 0.5) + 
    ggpubr::stat_compare_means(comparisons = list(c("A", "B")))

预期输出:

r ggplot2 plot boxplot
1个回答
0
投票

像这样吗?

比较需要是 x 轴上 2 个值的名称或索引,因此使用

facet_wrap
而不是躲避。

library(tidyverse)
library(ggsignif)

set.seed(123)

expr_data <- data.frame(
  cell_line_name = rep(c("CL1", "CL2"), each = 50),
  GSE_id = rep(c("GSE1", "GSE2"), each = 50),
  model = rep(c("M1", "M2"), each = 50),
  expr = rnorm(100),
  age_group = sample(c('A', 'B'), 100, replace = TRUE)
)
  
expr_data |> 
  mutate(x = paste0(cell_line_name, '_', GSE_id, '_', model)) |> 
  ggplot(aes(x = age_group, y = expr, color = age_group)) + 
  geom_boxplot() + 
  geom_point(position = position_jitter(), alpha = 0.5) + 
  geom_signif(
    comparisons = list(c("A", "B")),
    map_signif_level = TRUE, textsize = 6
  ) +
  facet_wrap(~x, scales = "free_x", strip.position = "bottom") +
  ylim(c(NA, 3)) +
  labs(x = "Axis Title") +
  theme_bw() +
  theme(
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank()
  )

创建于 2024-04-20,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.