压缩facet_wrap图(ggplot)

时间:2018-07-13 19:11:46

标签: r ggplot2 facet-wrap

是否有一种方法可以压缩使用facet_wrap的ggplot?这是我要压缩的情节示例。理想情况下,最左边的地块(年龄类别)应有更大的间距,而其余的地块则更为紧凑。 enter image description here

这是一个玩具示例。理想情况下,中间图将更窄。

mtcars %>% 
  mutate(cylinders = case_when(cyl == 4 ~ "Four cyl",
                               cyl == 6 ~ "Six cyl",
                               cyl == 8 ~ "Eight cyl"),
         cylinders = reorder(cylinders, cyl, mean)) %>% 
  mutate(engine = case_when(vs == 1 ~ "Manual",
                            vs == 0 ~ "Automatic")) %>% 
  mutate(gears = case_when(gear == 3 ~ "Three",
                           gear == 4 ~ "Four",
                           gear == 5 ~ "Five"),
         gears = reorder(gears, gear, mean)) %>% 
  select(cylinders, engine, gears) %>%
  gather(key, value) %>%
  count(key, value) %>%
  ggplot(aes(x = value, y = n)) +
  geom_bar(stat = "identity", width = 0.3) +
  facet_wrap(~ key, scales = "free_x", ncol = 3)

enter image description here

0 个答案:

没有答案