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

如何在ggplot中基于Grp1与Grp2两组分类并区分展示数据?

在ggplot中按组别区分展示通路数据的解决方案

问题背景

你的数据集包含10条通路数据,分为Grp1(pathway1-5)和Grp2(pathway6-10)两组,现有ggplot代码生成了以Zscore为x轴、通路ID为y轴的点图,点颜色区分Zscore正负、大小对应Hit数,现在需要在图中直观区分Grp1和Grp2两组。

你的数据集:

df <- structure(list(ID = c("pathway1", "pathway2", "pathway3", "pathway4", 
"pathway5", "pathway6", "pathway7", "pathway8", "pathway9", "pathway10"
), Hit = c(4L, 3L, 3L, 7L, 9L, 6L, 4L, 13L, 6L, 16L), Zscore = c(-1.3, 
-2, 2, 3, 1, -1.3, -2, 2, 3, 1), Group = c("Grp1", "Grp1", "Grp1", 
"Grp1", "Grp1", "Grp2", "Grp2", "Grp2", "Grp2", "Grp2")), class = "data.frame", row.names = c(NA, 
-10L))

你的现有代码:

f <- ggplot(df, aes(Zscore, ID))
f1 = f + geom_point(aes(colour = Zscore > 0, size = Hit)) +
  scale_color_manual(
    name = 'Z-score',
    labels = c("Suppressed", "Activated"),
    values = c(`FALSE` = 'navy', `TRUE` = 'red')
  ) +
  geom_vline(xintercept=0, size=0.2, colour="blue", linetype="dotted") +
  scale_size_continuous(breaks = c(0,1,2,3,5)) +
  theme(panel.background=element_rect(fill="gray95", colour="gray95"),
        panel.grid.major=element_line(size=0.1,linetype='solid', colour="gray90"), 
        panel.grid.minor=element_line(size=0.1,linetype='solid', colour="gray90"),
        axis.title.y=element_blank()) +
  expand_limits(x=c(-3,3)) +
  scale_x_continuous(name = 'Z-score', breaks = pretty(df$Zscore)) +
  scale_y_discrete(limits=rev(df$ID)) + theme(axis.text = element_text(face="bold"))
f1

三种实现分组区分的方法

方法1:分面展示(最直观的分组方式)

通过facet_wrap把两组拆分成上下两个子图,每组单独展示,互不干扰。修改代码时注意调整y轴顺序,确保每组内的通路顺序正确:

# 先按Group和ID排序,保证分面后通路顺序正确
df_sorted <- df %>% arrange(Group, ID)

ggplot(df_sorted, aes(Zscore, ID)) +
  geom_point(aes(colour = Zscore > 0, size = Hit)) +
  scale_color_manual(
    name = 'Z-score',
    labels = c("Suppressed", "Activated"),
    values = c(`FALSE` = 'navy', `TRUE` = 'red')
  ) +
  geom_vline(xintercept=0, size=0.2, colour="blue", linetype="dotted") +
  scale_size_continuous(breaks = c(0,1,2,3,5)) +
  theme(panel.background=element_rect(fill="gray95", colour="gray95"),
        panel.grid.major=element_line(size=0.1,linetype='solid', colour="gray90"), 
        panel.grid.minor=element_line(size=0.1,linetype='solid', colour="gray90"),
        axis.title.y=element_blank(),
        strip.background = element_rect(fill = "lightgray"), # 分面标题背景色
        strip.text = element_text(face = "bold")) + # 分面标题加粗
  expand_limits(x=c(-3,3)) +
  scale_x_continuous(name = 'Z-score', breaks = pretty(df$Zscore)) +
  scale_y_discrete(limits=rev(df_sorted$ID)) + 
  theme(axis.text = element_text(face="bold")) +
  facet_wrap(~Group, scales = "free_y", ncol = 1) # 按Group分面,y轴独立

方法2:同一图中添加组别背景色块

如果不想拆分图,可以给不同组的y轴区域添加不同背景色,实现分组区分:

# 计算每个通路对应的y轴位置,生成背景数据框
df$y_pos <- as.integer(factor(df$ID, levels = rev(df$ID)))
group_bounds <- df %>% 
  group_by(Group) %>% 
  summarize(y_min = min(y_pos) - 0.5, y_max = max(y_pos) + 0.5)

ggplot(df, aes(Zscore, ID)) +
  # 添加背景色块
  geom_rect(data = group_bounds, aes(xmin = -Inf, xmax = Inf, ymin = y_min, ymax = y_max, fill = Group),
            alpha = 0.1) +
  scale_fill_manual(values = c(Grp1 = "lightblue", Grp2 = "lightgreen")) +
  # 原有图层
  geom_point(aes(colour = Zscore > 0, size = Hit)) +
  scale_color_manual(
    name = 'Z-score',
    labels = c("Suppressed", "Activated"),
    values = c(`FALSE` = 'navy', `TRUE` = 'red')
  ) +
  geom_vline(xintercept=0, size=0.2, colour="blue", linetype="dotted") +
  scale_size_continuous(breaks = c(0,1,2,3,5)) +
  theme(panel.background=element_rect(fill="gray95", colour="gray95"),
        panel.grid.major=element_line(size=0.1,linetype='solid', colour="gray90"), 
        panel.grid.minor=element_line(size=0.1,linetype='solid', colour="gray90"),
        axis.title.y=element_blank()) +
  expand_limits(x=c(-3,3)) +
  scale_x_continuous(name = 'Z-score', breaks = pretty(df$Zscore)) +
  scale_y_discrete(limits=rev(df$ID)) + 
  theme(axis.text = element_text(face="bold")) +
  labs(fill = "Group") # 添加分组图例标题

方法3:用点形状区分组别

在原有颜色和大小的基础上,给不同组的点设置不同形状,通过形状直观区分:

ggplot(df, aes(Zscore, ID)) +
  geom_point(aes(colour = Zscore > 0, size = Hit, shape = Group)) +
  scale_color_manual(
    name = 'Z-score',
    labels = c("Suppressed", "Activated"),
    values = c(`FALSE` = 'navy', `TRUE` = 'red')
  ) +
  scale_shape_manual(name = "Group", values = c(Grp1 = 16, Grp2 = 17)) + # 设置不同形状
  geom_vline(xintercept=0, size=0.2, colour="blue", linetype="dotted") +
  scale_size_continuous(breaks = c(0,1,2,3,5)) +
  theme(panel.background=element_rect(fill="gray95", colour="gray95"),
        panel.grid.major=element_line(size=0.1,linetype='solid', colour="gray90"), 
        panel.grid.minor=element_line(size=0.1,linetype='solid', colour="gray90"),
        axis.title.y=element_blank()) +
  expand_limits(x=c(-3,3)) +
  scale_x_continuous(name = 'Z-score', breaks = pretty(df$Zscore)) +
  scale_y_discrete(limits=rev(df$ID)) + 
  theme(axis.text = element_text(face="bold"))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 06:50:27