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

ggplot2右侧Y轴标题无法旋转的原因及解决方法咨询

ggplot右侧Y轴标题水平显示问题的原因与解决办法

问题原因

当Y轴设置在右侧时,ggplot会单独使用axis.title.y.right这个主题元素来控制右侧Y轴的标题样式,而非默认左侧Y轴对应的axis.title.y。所以直接修改axis.title.y的参数不会对右侧Y轴标题生效。

解决办法

针对axis.title.y.right元素设置样式参数,即可实现右侧Y轴标题的水平显示:

修正后的代码

ggplot() + 
  geom_function(fun = ~ -.x) + 
  scale_x_continuous(limits = c(-0.27, 0), name = "example") +
  scale_y_continuous(position = "right", limits = c(0, 0.27), name = "test") +
  theme_classic() +
  theme(
    axis.title.y.right = element_text(angle = 0, hjust = 0, vjust = 0.5), 
    axis.title.x = element_text(hjust = 0)
  )

参数调整说明

  • angle = 0:强制标题水平显示
  • hjust = 0:将标题左对齐(可根据需求改为0.5居中、1右对齐)
  • vjust = 0.5:让标题在Y轴的垂直中心位置,避免出现偏移

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 18:15:48