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

如何修改ggplot2中geom_col生成的图例标签及标题?

问题描述

现有如下GSEA数据集:

structure(list(ID = c("ab1", "ab2", "ab3", "ab4", "ab5", "ab6", "ab7", "ab8", "ab9", "ab10", "ab11", "ab12", "ab13", "ab14", "ab15", "ab16", "ab17", "ab18", "ab19", "ab20"), Count = c(183L, 37L, 15L, 30L, 38L, 98L, 49L, 58L, 43L, 49L, 98L, 10L, 15L, 83L, 14L, 5L, 129L, 69L, 32L, 19L), NES = c(3.115511918, 2.65379757, 2.385595991, 2.310872715, 2.166809058, 2.157225827, 2.112706611, -1.982794655, -1.96510639, -1.890716501, -1.845158581, 1.843252451, -1.833650877, -1.832752633, 1.830915101, 1.827824231, -1.804884734, -1.79943276, -1.786694788, -1.779892361), pvalue = c(1.4e-43, 6.72e-12, 3.55e-07, 6.6e-08, 8.33e-07, 8.16e-14, 1.31e-08, 1.01e-06, 7.33e-06, 5.55e-05, 8.32e-06, 0.002090754, 0.0008865, 1.06e-06, 0.003576374, 0.005822333, 7.09e-08, 3.15e-05, 0.000196647, 0.001113372), REG = c("up", "up", "up", "up", "up", "up", "up", "down", "down", "down", "down", "up", "down", "down", "up", "up", "down", "down", "down", "down"), ABS = c(3.115511918, 2.65379757, 2.385595991, 2.310872715, 2.166809058, 2.157225827, 2.112706611, 1.982794655, 1.96510639, 1.890716501, 1.845158581, 1.843252451, 1.833650877, 1.832752633, 1.830915101, 1.827824231, 1.804884734, 1.79943276, 1.786694788, 1.779892361)), class = "data.frame", row.names = c(NA, -20L))

使用以下ggplot2代码绘制柱形图:

ggplot(gsea, aes(reorder(ID, NES), NES)) +
  geom_col(aes(fill=NES<0)) + 
scale_color_manual(name = 'NES', labels = c("Suppressed", "Activated"), values = c(`FALSE` = 'green', `TRUE` = 'red'))+
  coord_flip() +
  labs(x="Pathway", y="Normalized Enrichment Score",
       title="Curated pathways GSEA") + 
  theme_minimal()

当前图例标题为“NES<0”,标签为TRUE/FALSE,需要修改为:

  • 图例标题:regulation
  • TRUE对应标签:Suppressed
  • FALSE对应标签:Activated
解决方案

核心修改点:

  • 原代码误用了scale_color_manual,但geom_col映射的是fill美学,需替换为scale_fill_manual
  • 在scale_fill_manual中直接指定图例标题、标签和对应颜色

修改后的完整代码:

ggplot(gsea, aes(reorder(ID, NES), NES)) +
  geom_col(aes(fill = NES < 0)) + 
  # 替换为scale_fill_manual,设置标题、标签和颜色
  scale_fill_manual(
    name = "regulation",
    labels = c("Activated", "Suppressed"), # 顺序对应FALSE、TRUE的取值
    values = c(`FALSE` = 'green', `TRUE` = 'red')
  ) +
  coord_flip() +
  labs(
    x = "Pathway", 
    y = "Normalized Enrichment Score",
    title = "Curated pathways GSEA"
  ) + 
  theme_minimal()

另一种更直观的方法(提前转换因子)

可以先将NES < 0的结果转换为带有明确标签的因子,让代码逻辑更清晰:

# 预处理数据集,添加分组列
gsea$regulation <- factor(
  gsea$NES < 0,
  levels = c(FALSE, TRUE),
  labels = c("Activated", "Suppressed")
)

# 绘图代码
ggplot(gsea, aes(reorder(ID, NES), NES)) +
  geom_col(aes(fill = regulation)) + 
  scale_fill_manual(
    name = "regulation",
    values = c("Activated" = 'green', "Suppressed" = 'red')
  ) +
  coord_flip() +
  labs(
    x = "Pathway", 
    y = "Normalized Enrichment Score",
    title = "Curated pathways GSEA"
  ) + 
  theme_minimal()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 05:05:36