2016-12-13 140 views
4

我利用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)) 

Example dual Axis plot with both axes labels rotated 它在任何我读过的文件没有提到(如scale_continuousthemes)如何实现只有一个轴旋转的。

我的要求是,我希望应用于我的数据的一些标签在水平放置时很长并且重叠,通过旋转它们我可以避免这种情况,但是我希望将水平方向保留在底部轴上。

回答

4

如果您运行的是最新的dev versionggplot2,您可以使用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 = 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.top= element_text(angle = 45, hjust = 0)) 

enter image description here

+0

感谢您的解决方案斧头兵,我跨偶然发现一个小时前,它的伟大工程这个简单的例子,但我现在挠我的脑袋,为什么它不适用于我的实际数据。很高兴知道我在正确的轨道上,欢呼。 – slackline

+0

解决,顺序很重要,我有一个后续'+主题()'适用于我的情节,这是清理'主题(axis.text.x.top = element_text(angle = 45,hjust = 0))'以防其他人忘记该命令,随后调用'theme()'可以清除以前的调用)。 – slackline