【问题标题】:ggplot facet_wrap with labels only at the top and left sideggplot facet_wrap 仅在顶部和左侧带有标签
【发布时间】:2019-12-31 08:54:05
【问题描述】:

我正在尝试使用facet_wrap 参数在ggplot 中创建图表。

但是,我不希望在每个小图上都有标签,我只希望在图的顶部和左侧有一个标签。

例如在下图中,我想在顶部有标签 SI2、SI1、WS2,在左边有标签 D、E、F。

library(tidyverse)

df <- diamonds %>% 
  select(cut, color, clarity, price) %>% 
  filter(clarity %in% c("SI2", "SI1", "VVS2")) %>% 
  filter(color %in% c("D", "E", "F"))


df %>% 
  ggplot(aes(cut, price)) + 
  geom_boxplot() +
  facet_wrap(~color + clarity)

【问题讨论】:

  • 使用facet_gridggplot(df, aes(cut, price)) + geom_boxplot() + facet_grid(rows = vars(color) , cols = vars(clarity), switch = "y")
  • 谢谢!将其添加为答案,以便我可以投票。另外,两个问题:1.有没有办法让标签位于 y 轴的左侧? 2. facet_grid 和 facet_wrap 有什么区别?

标签: r ggplot2 facet-wrap


【解决方案1】:

答案是@markus 的评论和theme() 中的参数strip.placement = "outside" 之间的合并。

df %>% 
  ggplot(aes(cut, price)) + 
  geom_boxplot() +
  facet_grid(rows = vars(color) , 
               cols = vars(clarity), 
             switch = "y") + 
  theme(strip.placement = "outside")

有关facet_wrapfacet_grid 之间差异的信息可以在here 找到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 2018-12-24
    相关资源
    最近更新 更多