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

为什么ggplot2中指定Y轴标签文本后仍显示数字1?

问题原因

你的代码中scale_y_continuous函数的参数末尾多了一个冗余的,1,这是Y轴标签显示为数字1的核心原因:
scale_y_continuous的第一个位置参数为Y轴名称(name参数),你在breaks参数后额外添加的未命名1会被R按参数传入顺序识别为name参数的取值,优先级高于labs()中设置的Y轴标签,因此无论怎么调整labs的Y轴配置都会被覆盖。

修正后的代码

只需要删除scale_y_continuous中breaks参数后的冗余,1即可:

ggplot(twohour, aes(x = Treatmentinorder, y = Total, fill = Treatment)) + 
  scale_y_continuous(limits=c(-1,500),
                     breaks = seq(0, 500, by = 100)) +
  
  geom_boxplot(notch=F,  
               outlier.shape= NA, 
               width=0.7,
               lwd=0.5,
               fatten=1,
               color="black",
               position=position_dodge(0.7)) +
stat_summary(fun.y="mean", geom= "point", size=2, position=position_dodge(1), color="black") +
  labs (x= "Heatwave exposure group", y= "20 day reproductive output") +
  geom_jitter(shape=1, size=1.5, position=position_jitter(0.25))+
  
  
theme_classic() + 
  theme(legend.position="none", 
        axis.text.x=element_text(color="black", size=12),
        axis.text.y=element_text(color="black", size=12),
        axis.title.x=element_text(face = "bold", size=12),
        axis.title.y=element_text(face = "bold", size=12))

避坑提示

编写ggplot2的scale类函数时,尽量显式标注参数名,避免因位置参数传参顺序不明确导致的隐蔽错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 01:12:00