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

使用patchwork合并ggplot2图表时如何生成统一共享图例?

解决ggplot2+patchwork合并图表的统一图例问题

问题根源在于两个数据集的phenotype存在部分重叠但不完全一致,你之前分别给每个图表单独设置形状映射,导致相同phenotype在两个图里可能对应不同形状,最终合并后图例混乱。要实现统一共享图例,核心是让两个图表使用完全一致的形状映射规则,具体操作如下:

步骤说明:

  • 先收集两个数据集里所有phenotype的唯一值,确保覆盖全部类别
  • 为每个唯一的phenotype分配固定的形状编号,保证同一个phenotype在两个图里对应同一个形状
  • 两个图表都调用这个统一的形状映射,再用patchwork合并并收集图例

修改后的完整代码:

library(ggplot2)
library(patchwork)

set.seed(123)
top <- data.frame(
  freq_a = runif(65, 0, 5),
  freq_b = runif(65, 0, 5),
  phenotype = rep(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", 'M'), each = 5),
  gene = rep(c("X", "Y", "Z"), each = 20/3, length.out = 65)
)

bottom <- data.frame(
  freq_a = runif(70, 0, 5),
  freq_b = runif(70, 0, 5),
  phenotype = rep(c("N", "O", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", 'M'), each = 5),
  gene = rep(c("X", "Y", "Z"), each = 20/3, length.out = 70)
)

# 收集所有phenotype的唯一值,统一排序
all_phenotypes <- sort(unique(c(top$phenotype, bottom$phenotype)))
# 为每个phenotype分配固定形状值
shape_values <- 1:length(all_phenotypes)
names(shape_values) <- all_phenotypes

# 绘制顶部图表,使用统一的形状映射
p_top <- ggplot(top, aes(x=freq_a, y=freq_b, color=gene, shape=phenotype)) +
  geom_point() +
  scale_shape_manual(values = shape_values)

# 绘制底部图表,使用完全相同的形状映射
p_bottom <- ggplot(bottom, aes(x=freq_a, y=freq_b, color=gene, shape=phenotype)) +
  geom_point() +
  scale_shape_manual(values = shape_values)

# 合并图表并收集统一图例
combined <- p_top + p_bottom & 
  theme(legend.position="bottom", legend.box="vertical")
combined <- combined + 
  plot_layout(guides = "collect") + 
  plot_annotation(tag_levels = 'A')

# 查看结果
print(combined)

关键修改点:

  • 新增all_phenotypes整合两个数据集的所有phenotype类别,排序后保证顺序固定
  • shape_values为每个phenotype绑定固定的形状编号,避免各自映射时的混乱
  • 两个图表都使用同一个scale_shape_manual(values = shape_values),确保形状映射规则完全一致

这样合并后的图表就会生成一个包含所有phenotype的统一共享图例,且相同类别在两个图里的形状完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 14:58:24