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

如何移除ggplot2中facet_wrap分组图的Y轴刻度

解决方案

方案1:移除右侧Y轴(若红色标记为右侧轴刻度)

你的代码里通过sec.axis = sec_axis(~.*1)创建了和左侧完全一致的右侧Y轴,若红色标记的是这部分刻度,直接删掉该行即可移除右侧轴及刻度:

ggplot(df1, aes(x=Specie, y=mean, fill=Treatment))+
  geom_bar(stat="identity", position=position_dodge(), color="black")+
  facet_wrap(~ variable, scales = "free")+
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=0.2,position=position_dodge(0.9), 
                color="blue")+
  scale_y_continuous(labels = number_format(accuracy = 0.01))

方案2:保留右侧轴框架但隐藏刻度

如果需要保留右侧轴的线条,仅移除刻度和标签,可给sec_axis添加guide参数隐藏对应元素:

ggplot(df1, aes(x=Specie, y=mean, fill=Treatment))+
  geom_bar(stat="identity", position=position_dodge(), color="black")+
  facet_wrap(~ variable, scales = "free")+
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=0.2,position=position_dodge(0.9), 
                color="blue")+
  scale_y_continuous(labels = number_format(accuracy = 0.01),
                     sec.axis = sec_axis(~.*1, guide = guide_axis(theme = element_blank())))

方案3:移除左侧Y轴特定刻度(若红色标记为左侧部分刻度)

如果红色标记的是左侧Y轴的部分刻度,可通过breaks参数指定仅保留需要显示的刻度值,其余自动移除:

ggplot(df1, aes(x=Specie, y=mean, fill=Treatment))+
  geom_bar(stat="identity", position=position_dodge(), color="black")+
  facet_wrap(~ variable, scales = "free")+
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=0.2,position=position_dodge(0.9), 
                color="blue")+
  scale_y_continuous(labels = number_format(accuracy = 0.01),
                     breaks = c(0, 0.5, 1), # 替换为你需要保留的刻度值
                     sec.axis = sec_axis(~.*1))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 00:06:21