You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R的ComplexUpset Upset图中添加基因调控方向注释图表?

解决ComplexUpset堆叠条形图注释对齐问题

你当前的问题在于没有利用ComplexUpset的annotations参数将方向比例图与Upset的每个交集绑定,而是简单拼接了两个独立图表,导致无法对齐。下面是修正后的完整代码,实现类似官方vignette中MPAA Rating注释的精准对齐效果:

核心思路

通过annotations参数传入自定义函数,该函数会自动接收每个Upset交集对应的基因子集,动态计算并绘制该子集的方向比例堆叠图,确保与每个交集一一对应。

完整代码

# 加载必要库
library(ComplexUpset)
library(ggplot2)
library(dplyr)
library(patchwork)

# 生成示例数据(保留你的原始逻辑)
set.seed(45)
example_df <- data.frame(geneid=paste('gene_', 1:15000, sep = ''),
                         comparison_1=sample(c(T, F), size = 15000, replace = T, prob = c(.7, .3)),
                         comparison_2=sample(c(T, F), size = 15000, replace = T, prob = c(.40, .6)),
                         comparison_3=sample(c(T, F), size = 15000, replace = T, prob = c(.25, .75)),
                         comparison_4=sample(c(T, F), size = 15000, replace = T, prob = c(.12, .88)), 
                         fill_comparison_1=sample(c('up', 'down'), size = 15000, replace = T, prob = c(.60,.40)),
                         fill_comparison_2=sample(c('up', 'down'), size = 15000, replace = T, prob = c(.55,.45)), 
                         fill_comparison_3=sample(c('up', 'down'), size = 15000, replace = T, prob = c(.50,.50)), 
                         fill_comparison_4=sample(c('up', 'down'), size = 15000, replace = T, prob = c(.65,.35)))

# 定义注释生成函数:针对每个交集子集绘制方向比例堆叠图
direction_annotation <- function(data) {
  # 处理空交集,避免报错
  if (nrow(data) == 0) {
    return(ggplot() + theme_void())
  }
  
  # 获取当前交集对应的比较列
  comp_cols <- grep('^comparison_', colnames(data), value = TRUE)
  comp_cols <- comp_cols[sapply(data[, comp_cols], function(x) all(x))]
  
  # 获取对应的方向列
  fill_cols <- paste0('fill_', comp_cols)
  
  # 根据交集类型计算方向类别
  if (length(comp_cols) == 1) {
    # 单比较交集:直接使用该组的方向
    data$category <- data[, fill_cols]
  } else if (length(comp_cols) == 2) {
    # 双比较交集:判断一致/相反方向
    fill1 <- data[, fill_cols[1]]
    fill2 <- data[, fill_cols[2]]
    data$category <- case_when(
      fill1 == 'up' & fill2 == 'up' ~ 'up',
      fill1 == 'down' & fill2 == 'down' ~ 'down',
      TRUE ~ 'opposite'
    )
  }
  
  # 绘制堆叠比例条形图
  ggplot(data, aes(x = '', fill = category)) +
    geom_bar(position = 'fill', width = 1) +
    scale_fill_manual(values = c('up' = '#2ecc71', 'down' = '#e74c3c', 'opposite' = '#95a5a6')) +
    theme_void() +
    theme(legend.position = 'none')
}

# 绘制带注释的Upset图
ex_upset <- upset(
  example_df,
  intersect = c(paste('comparison', 1:4, sep='_')),
  mode = 'inclusive_intersection',
  max_degree = 4,
  keep_empty_groups = TRUE,
  sort_sets = FALSE,
  intersections = list(
    'comparison_1', 
    'comparison_2', 
    'comparison_3', 
    'comparison_4', 
    c('comparison_1','comparison_2'), 
    c('comparison_3','comparison_4'), 
    c('comparison_1','comparison_3'), 
    c('comparison_2','comparison_4')
  ),
  # 集成方向注释
  annotations = list(
    "方向分布" = direction_annotation
  ),
  # 调整注释区域尺寸
  annotation_height = 0.2,
  width_ratio = 0.1
)

# 生成独立图例(可选)
legend_plot <- ggplot(data.frame(category = c('up', 'down', 'opposite')), aes(x = '', fill = category)) +
  geom_bar() +
  scale_fill_manual(values = c('up' = '#2ecc71', 'down' = '#e74c3c', 'opposite' = '#95a5a6')) +
  theme_void() +
  theme(legend.position = 'bottom') +
  guides(fill = guide_legend(title = '方向'))

# 组合Upset图与图例
ex_upset + legend_plot + plot_layout(widths = c(10, 1))

关键优化点

  • 利用annotations参数实现注释与交集的动态绑定,确保完全对齐
  • 处理空交集场景,避免绘图报错
  • 针对单/双比较交集分别计算方向类别
  • 通过annotation_height和width_ratio灵活控制注释区域大小
  • 添加独立图例明确颜色与方向的对应关系

内容的提问来源于stack exchange,提问作者Panchito

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 23:35:49