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

如何让ggplot X轴标签按ONTOLOGY分类着色?标签显示异常求助

解决ggplot中GO富集柱状图X轴标签按ONTOLOGY分类着色的问题

问题描述

绘制GO富集分析柱状图时,需要让X轴标签按ONTOLOGY(BP/CC/MF)分类设置对应颜色。原生axis.text.x无法实现按分类着色,因此隐藏了原生X轴标签,改用geom_text()添加标签,但标签只能显示在绘图区域内,无法像原生轴标签一样显示在轴下方,达不到预期效果。

当前代码

# 设置颜色
ontology_colors <- c("BP" = "#339970", 
                     "CC" = "#ff6633",
                     "MF" = "#9999ff")
plot<- ggplot(data = eGO_top10, aes(x = Description, y = enrichment_score, fill = ONTOLOGY)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.2)) +
  geom_text(aes(label = sprintf("%.2f", enrichment_score)),  # 显示两位小数的y值
            position = position_dodge(width = 0.4), vjust = -0.3,size = 2) + 
  geom_text(data = eGO_top10, aes(x = Description, y = 1, label = Description, color = ONTOLOGY),  
            angle = 75, hjust = 1, vjust = 10, size = 3.5, position = position_nudge(y = -0.5)) +
  scale_fill_manual(values = ontology_colors) +
  scale_color_manual(values = ontology_colors)+
  # facet_wrap(~ ONTOLOGY, scales = "free_x", ncol = 1) +  # 按照ONTOLOGY分类
  facet_grid(~ ONTOLOGY,scales = "free_x", space = "fixed", margins = FALSE) +  
  labs(x = "", y = "Enrichment Score", fill = "ONTOLOGY",title = "") +  
  theme(plot.background = element_rect(fill = "white"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        strip.text = element_blank(),
        panel.background = element_rect(fill = "white"),
        panel.border = element_rect(fill = NA, colour = "white"),
        # axis.text.x = element_text(angle = 75, hjust = 1),
        axis.text.x = element_blank(),
        panel.spacing = unit(0,"cm"),
        plot.margin = margin(1,0,0,1,"cm"),
        axis.line = element_line(color = "black"))  

ggsave("GO_barplot_Enrichment_Score.tiff", plot, width = 10, height = 8, units = "in", dpi = 600)

效果对比

  • 当前异常效果:标签被限制在绘图区域内,无法显示在X轴下方的正确位置
  • 预期效果:X轴标签按BP/CC/MF对应颜色显示在轴下方,布局整齐

解决方案

推荐两种可行方法:

方法1:用ggtext包的element_markdown实现轴标签着色

ggtext支持在轴标签中使用Markdown语法,直接给不同ONTOLOGY的标签添加颜色样式,无需隐藏原生轴标签,逻辑更贴合ggplot原生机制。

步骤示例:

library(ggtext)

# 为每个Description添加对应ONTOLOGY颜色的Markdown格式标签
eGO_top10$label_colored <- paste0(
  "<span style='color:", ontology_colors[eGO_top10$ONTOLOGY], "'>", 
  eGO_top10$Description, 
  "</span>"
)

plot <- ggplot(data = eGO_top10, aes(x = label_colored, y = enrichment_score, fill = ONTOLOGY)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.2)) +
  geom_text(aes(label = sprintf("%.2f", enrichment_score)),  
            position = position_dodge(width = 0.4), vjust = -0.3, size = 2) + 
  scale_fill_manual(values = ontology_colors) +
  facet_grid(~ ONTOLOGY, scales = "free_x", space = "fixed", margins = FALSE) +  
  labs(x = "", y = "Enrichment Score", fill = "ONTOLOGY", title = "") +  
  theme(plot.background = element_rect(fill = "white"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        strip.text = element_blank(),
        panel.background = element_rect(fill = "white"),
        panel.border = element_rect(fill = NA, colour = "white"),
        axis.text.x = element_markdown(angle = 75, hjust = 1, size = 3.5),  # 启用markdown渲染
        panel.spacing = unit(0,"cm"),
        plot.margin = margin(1, 1, 2, 1, "cm"),  # 增加底部边距容纳倾斜标签
        axis.line = element_line(color = "black"))  

ggsave("GO_barplot_Enrichment_Score.tiff", plot, width = 10, height = 8, units = "in", dpi = 600)

方法2:调整geom_text()位置并扩展绘图区域

若不想引入额外包,可通过调整标签位置和绘图边距,让标签显示在轴下方:

plot<- ggplot(data = eGO_top10, aes(x = Description, y = enrichment_score, fill = ONTOLOGY)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.2)) +
  geom_text(aes(label = sprintf("%.2f", enrichment_score)),  
            position = position_dodge(width = 0.4), vjust = -0.3,size = 2) + 
  # 调整y值到轴下方,修正对齐方式
  geom_text(data = eGO_top10, aes(x = Description, y = -0.8, label = Description, color = ONTOLOGY),  
            angle = 75, hjust = 1, vjust = 0, size = 3.5) +
  scale_fill_manual(values = ontology_colors) +
  scale_color_manual(values = ontology_colors)+
  scale_y_continuous(expand = expansion(mult = c(0.1, 0.1), add = c(1.2, 0))) + # 扩展y轴下限
  facet_grid(~ ONTOLOGY,scales = "free_x", space = "fixed", margins = FALSE) +  
  labs(x = "", y = "Enrichment Score", fill = "ONTOLOGY",title = "") +  
  theme(plot.background = element_rect(fill = "white"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        strip.text = element_blank(),
        panel.background = element_rect(fill = "white"),
        panel.border = element_rect(fill = NA, colour = "white"),
        axis.text.x = element_blank(),
        panel.spacing = unit(0,"cm"),
        plot.margin = margin(1, 1, 3, 1, "cm"), # 大幅增加底部边距
        axis.line = element_line(color = "black"))  

ggsave("GO_barplot_Enrichment_Score.tiff", plot, width = 10, height = 8, units = "in", dpi = 600)

说明

方法1更简洁稳定,推荐优先使用;方法2无需额外依赖,但需要手动调整边距和轴范围,灵活性稍弱。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 15:53:20