【问题标题】:Align axis label on the right with ggplot2将右侧的轴标签与 ggplot2 对齐
【发布时间】:2016-09-26 01:33:10
【问题描述】:

考虑以下

d = data.frame(y=rnorm(120), 
               x=rep(c("bar", "long category name", "foo"), each=40))

ggplot(d,aes(x=x,y=y)) + 
    geom_boxplot() + 
    theme(axis.text.x=element_text(size=15, angle=90))

x 轴标签按标签中心对齐。是否可以自动向右对齐,以便每个标签都在图形下方结束?

【问题讨论】:

    标签: r graph graphics ggplot2


    【解决方案1】:

    这正是hjustvjust 参数在ggplot 中的用途。它们分别控制水平和垂直对齐,范围从 0 到 1。有关对齐及其值的更多详细信息,请参阅此问题 (What do hjust and vjust do when making a plot using ggplot?)。

    要以您想要的方式获取标签:

    • hjust = 0.95(在标签和轴之间留一些空间)
    • vjust = 0.2(在这种情况下将它们居中)

    ggplot(d,aes(x=x,y=y)) + geom_boxplot() + 
           theme(axis.text.x=element_text(size=15, angle=90,hjust=0.95,vjust=0.2))
    

    【讨论】:

      【解决方案2】:

      或者,翻转轴,您的客户会感谢您并减少颈部疼痛(另外,我发现大多数箱线图更容易解释这个方向):

      ggplot(d, aes(x = x, y = y)) +
        geom_boxplot() + 
        coord_flip()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-11
        相关资源
        最近更新 更多