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

如何在ggplot2中移除scale_y_cut生成的子图标题及冗余刻度值

问题解决方案

多余标题修复

该问题是ggbreak包的已知兼容问题:scale_y_cut拆分Y轴生成截断分块时,若存在geom_hline元素,会将截断分块的内部标识误渲染为可见的额外标题,你只需要在theme()中添加一行代码隐藏该专有元素即可,无需删除geom_hline语句。

冗余刻度移除

你可以通过scale_y_continuous()的breaks参数手动指定需要展示的Y轴刻度,跳过0-6、50-100区间的密集刻度即可。

修改后完整代码
library(ggsignif)
library(tidyverse)
library(ggpubr)
library(readxl)
library(rstatix)
library(stringr)
library(ggbreak)

CO2 <- CO2

ggplot(CO2, aes(x=Plant,y=uptake, fill=Plant))+
  geom_boxplot(outlier.alpha = 0.25, outlier.color = "red") +
  geom_point(alpha = 0.25, size = 2 )+
  facet_wrap(~Type)+
  stat_compare_means(comparisons = list(c("Qn1","Qn2")), label.y = 105  )+
  stat_compare_means(comparisons = list(c("Qc1","Qc2")), label.y = 110)+
  stat_compare_means(comparisons = list(c("Mn1","Mn2")), label.y = 115)+
  theme(axis.text.x = element_text(angle = 20),
        # 隐藏ggbreak生成的多余标题
        ggbreak.panel.title = element_blank())+
  geom_hline(yintercept=100, linetype="dashed", color="red", size=1)+
  geom_hline(yintercept=5, linetype="dashed", color="red", size=1)+
  geom_hline(yintercept=130)+
  geom_hline(yintercept=0)+
  labs(y="uptake")+
  # 自定义Y轴刻度,跳过密集区间,可根据需求调整刻度取值
  scale_y_continuous(breaks = c(0,7,20,30,40,50,100,110,120,130))+
  scale_y_cut(breaks=c(7, 50, 95), which=c(1,2,3,4), scales=c(1,0.25,3,0.25))

注:将原代码中已被淘汰的aes_string替换为标准的aes写法,不影响功能,若你需要保留原写法可直接改回。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 19:36:03