选择性地从条形图中删除条形而不更改格式

时间:2017-05-18 17:59:19

标签: r ggplot2

我想从第一个图中再删一列而不改变两个图中条形的宽度或对齐方式。非常感谢任何帮助。

theme_min <- 
  theme(
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    panel.background = element_blank(), 
    panel.border = element_blank(),
    axis.line = element_line(colour = "black")
  )

# display 2 of 4 columns
mtcars %>% 
  mutate(
    am = factor(am, labels = c("auto", "manual")),
    vs = factor(vs, labels = c("V", "S"))
  ) %>% 
  filter(
    am == "auto"
  ) %>%
  ggplot(aes(x = am, y = mpg, fill = vs)) + 
  geom_col(position = position_dodge()) +
  scale_y_continuous(limits = c(0,35)) + 
  theme_min +
  scale_x_discrete(drop = FALSE)

2 of 4 bars

# display 1 of 4 columns
mtcars %>% 
  mutate(
    am = factor(am, labels = c("auto", "manual")),
    vs = factor(vs, labels = c("V", "S"))
  ) %>% 
  filter(
    am == "auto",
    vs == "V"
  ) %>%
  ggplot(aes(x = am, y = mpg, fill = vs)) + 
  geom_col(position = position_dodge()) +
  scale_y_continuous(limits = c(0,35)) + 
  theme_min +
  scale_x_discrete(drop = FALSE) +
  scale_fill_discrete(drop = FALSE)

1 of 4 bars displayed, but width is out of sync with image 1

1 个答案:

答案 0 :(得分:1)

我能够通过改变结果变量来选择性地删除列,因此对于你想要删除的列,它等于零。

queryString

one bar plot