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

如何在ggplot2的facet_wrap中旋转分类标题并调整位置?

调整ggplot2 facet_wrap面板标题至右侧并水平显示

要实现将面板分类标题水平放置在面板右侧的需求,只需修改facet_wrap()的参数并配合theme()设置即可完成,以下是修改后的完整代码:

library(ggplot2)

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  facet_wrap(vars(class), strip.position = "right") +
  theme(strip.text.y = element_text(angle = 0))

参数说明:

  • strip.position = "right":将面板标题的位置从默认顶部切换到右侧
  • strip.text.y = element_text(angle = 0):标题移到右侧后默认处于垂直旋转90度的状态,设置angle = 0可让标题变为水平显示

如果需要进一步调整标题对齐方式(比如左对齐),可在element_text()中添加hjust参数:

theme(strip.text.y = element_text(angle = 0, hjust = 0))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 04:20:22