【问题标题】:R: Align facet_grid() to the top of the gridR:将 facet_grid() 对齐到网格的顶部
【发布时间】:2020-08-27 17:54:49
【问题描述】:

我正在努力尝试在 R 中格式化绘图的 facet_grid。这是我的代码的可重现示例,其中的数据可从 R (ToothGrowth) 中的数据集包中获得。

library(ggplot2)

df <- ToothGrowth

bp <- ggplot(df, aes(x=dose, y=len, group=dose)) +
             geom_boxplot(aes(fill=dose)) +   
             facet_grid(supp ~ .) + 
             theme_minimal(base_size = 16) +   
             theme(legend.position = "none",
                   axis.title.x = element_blank(),
                   axis.title.y = element_blank(),
                   axis.ticks = element_blank(),
                   panel.grid.major.x = element_line(size = 0.25,
                                                     colour = "grey80"),
                   strip.text = element_text(size = 22, face = "bold"))

bp

通过代码我获得了第一张图片:

我想将 facet_grid 的标签对齐到网格图例的顶部,如第二张图片所示:

谢谢。

【问题讨论】:

    标签: r ggplot2 facet-grid


    【解决方案1】:

    将以下内容添加到您的绘图代码中:

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

    如果你想旋转标签,你可以这样做:

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

    (是的,vjusthjust 的事情令人困惑)

    p1 = bp + theme(strip.text.y=element_text(hjust=0))
    p2 = bp + theme(strip.text.y=element_text(vjust=1, angle=0))
    

    【讨论】:

    • 谢谢!我在玩 strip.text 但我没能做到,但你的解决方案完美无缺!
    猜你喜欢
    • 2021-08-19
    • 2020-06-27
    • 1970-01-01
    • 2013-12-21
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多