2016-12-13 142 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