我正在利用ggplot2中最近添加的辅助轴标签功能.我想只旋转辅助轴,但无法找到文档或计算出如何执行此操作.
它足够简单,可以使用...旋转所有文本
ggplot(mtcars, aes(x = wt, y = mpg, colour = mpg)) +
geom_point() +
scale_x_continuous(name = 'Bottom Axis',
sec.axis = sec_axis(trans = ~ .,
name = 'Top Axis',
breaks = c(2:5),
labels = c('Two Two', 'Three Three Three', 'Four Four Four Four', 'Five Five Five Five Five'))) +
## Rotate text of x-axis
theme(axis.text.x = element_text(angle = 90))
Run Code Online (Sandbox Code Playgroud)
在我读过的任何文档中都没有提到它(例如scale_continuous和themes)如何实现只有一个轴的旋转.
我要求这样做的动机是,我希望应用于我的数据的一些标签是长的并且在水平放置时重叠,通过旋转它我可以避免这种情况,但我希望在底部轴上保持水平方向.
如果您运行的是最新开发版本中ggplot2,你可以使用axis.text.x.top:
ggplot(mtcars, aes(x = wt, y = mpg, colour = mpg)) +
geom_point() +
scale_x_continuous(
name = 'Bottom Axis',
sec.axis = sec_axis(
trans = ~ .,
name = 'Top Axis',
breaks = 2:5,
labels = c('Two Two', 'Three Three Three', 'Four Four Four Four', 'Five Five Five Five Five')
)
) +
## Rotate text of x-axis
theme(axis.text.x.top = element_text(angle = 45, hjust = 0))
Run Code Online (Sandbox Code Playgroud)