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

基于Seurat Metadata复刻堆叠条形图:替换分组标签与调整间距问题

复刻堆叠条形图:添加分组彩色标签与增大组间距

解决方案思路

借助ggh4x包的嵌套分面功能可轻松实现分组标签的彩色背景,同时通过分面间距参数调整组间距离;若不想额外安装包,也可通过手动添加注释矩形的方式实现。


方法一:使用ggh4x包(推荐)

该方法简洁易维护,能完美匹配需求:

  1. 安装并加载依赖包:
install.packages("ggh4x")
library(ggh4x)
library(tidyverse)
  1. 预处理数据,确保分组和样本顺序正确:
# 按需求设置patient_ID的展示顺序,定义cohort的层级
meta_data <- meta_data %>%
  mutate(
    patient_ID = factor(patient_ID, levels = patient_ID_reorder),
    cohort = factor(cohort, levels = c("mono", "combo"))
  )
  1. 绘制带彩色分组标签的堆叠条形图:
# 定义cohort分组对应的背景色
cohort_colors <- c("mono" = "#E69F00", "combo" = "#56B4E9")

p <- ggplot(meta_data, aes(y = patient_ID, fill = celltype)) +
  geom_bar(position = "fill") +
  # 嵌套分面:按cohort分组,将标签移至y轴左侧,开放y轴间距调整权限
  facet_nested(~ cohort, scales = "free_y", space = "free_y", switch = "y") +
  # 设置分组标签样式:彩色背景+白色加粗文字
  theme(
    strip.background.y = element_rect(fill = cohort_colors),
    strip.text.y = element_text(color = "white", face = "bold", size = 10),
    # 增大两组间距,可根据需求调整数值
    panel.spacing.y = unit(1, "cm"),
    # 去除多余轴元素,匹配参考图样式
    axis.title.x = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank(),
    axis.title.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.text.y = element_text(size = 9),
    panel.background = element_blank(),
    plot.title = element_text(hjust = 0.5, size = 12)
  ) +
  scale_fill_brewer(palette = "Set1") +
  ggtitle("Cell type proportion")

print(p)

方法二:手动添加注释矩形(无需额外包)

若不想安装ggh4x,可通过annotation_custom手动绘制分组背景和标签:

  1. 预处理数据(同方法一)
  2. 计算分组对应的y轴范围,绘制图表:
# 计算每个cohort的样本数量
n_mono <- sum(meta_data$cohort == "mono")
n_combo <- sum(meta_data$cohort == "combo")
total_patients <- n_mono + n_combo

p <- ggplot(meta_data, aes(y = patient_ID, fill = celltype)) +
  geom_bar(position = "fill") +
  # 绘制mono组背景矩形
  annotation_custom(
    grob = rectGrob(gp = gpar(fill = "#E69F00", alpha = 1)),
    xmin = -Inf, xmax = Inf,
    ymin = 0.5, ymax = n_mono + 0.5
  ) +
  # 绘制combo组背景矩形
  annotation_custom(
    grob = rectGrob(gp = gpar(fill = "#56B4E9", alpha = 1)),
    xmin = -Inf, xmax = Inf,
    ymin = n_mono + 0.5, ymax = total_patients + 0.5
  ) +
  # 添加分组标签
  annotate("text", x = -0.1, y = (n_mono + 1)/2, 
           label = "mono", color = "white", fontface = "bold") +
  annotate("text", x = -0.1, y = n_mono + (n_combo + 1)/2, 
           label = "combo", color = "white", fontface = "bold") +
  # 调整x轴范围,避免标签被裁剪
  coord_cartesian(xlim = c(0, 1), clip = "off") +
  theme(
    axis.title.x = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank(),
    axis.title.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.text.y = element_text(size = 9),
    panel.background = element_blank(),
    plot.title = element_text(hjust = 0.5, size = 12),
    # 左侧留出空间放置分组标签
    plot.margin = margin(left = 30, unit = "pt")
  ) +
  scale_fill_brewer(palette = "Set1") +
  ggtitle("Cell type proportion")

print(p)

关键说明

  • 两种方法都需确保patient_ID的顺序正确,通过factor(levels = ...)预先定义层级是核心前提。
  • 方法一中的panel.spacing.y参数可灵活调整两组间距,数值越大间距越宽。
  • 可根据需求修改cohort_colors中的颜色值,匹配参考图配色。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 04:45:00