geom_wrap中缺少和不相关的轴标签

时间:2019-03-23 03:20:56

标签: r ggplot2 facet-wrap

上面两个方框图中缺少我1990和2010年的轴标签。我也希望删除1980、2000和2020标签,因为我只有1990和2010的数据。我尝试使用mutate函数:

df2 <- mutate(Year = Year %>% as.factor()) 

但是在此过程中,它消除了覆盖范围,这就是我绘制箱形图所依据的范围。先感谢您。

enter image description here

df <- tibble::tribble(
    ~Year,    ~Reach, ~Quantity,                ~Bar.Type,
     1990, "Reach 1",     24,            "Lateral bar",
     1990, "Reach 1",     16, "Bar accreted to island",
     1990, "Reach 1",      8,        "Mid channel bar",
     1990, "Reach 2",     47,            "Lateral bar",
     1990, "Reach 2",     77, "Bar accreted to island",
     1990, "Reach 2",     36,        "Mid channel bar",
     1990, "Reach 3",     18,            "Lateral bar",
     1990, "Reach 3",     12, "Bar accreted to island",
     1990, "Reach 3",      6,        "Mid channel bar",
     1990, "Reach 4",     16,            "Lateral bar",
     1990, "Reach 4",     29, "Bar accreted to island",
     1990, "Reach 4",     12,        "Mid channel bar",
     2010, "Reach 1",     21,            "Lateral bar",
     2010, "Reach 1",     23, "Bar accreted to island",
     2010, "Reach 1",     13,        "Mid channel bar",
     2010, "Reach 2",     48,            "Lateral bar",
     2010, "Reach 2",    116, "Bar accreted to island",
     2010, "Reach 2",     65,        "Mid channel bar",
     2010, "Reach 3",     17,            "Lateral bar",
     2010, "Reach 3",     14, "Bar accreted to island",
     2010, "Reach 3",     10,        "Mid channel bar",
     2010, "Reach 4",     16,            "Lateral bar",
     2010, "Reach 4",     36, "Bar accreted to island",
     2010, "Reach 4",     14,        "Mid channel bar"
    )

ggplot(df, aes(x = Year, Quantity))+ 
  geom_bar(stat = "identity", aes(fill = Bar.Type))+
  facet_wrap(~Reach, ncol=2)+
  scale_fill_brewer(palette = "Blues",
                    labels = c("Lateral bar", "Bar accreted to island",
                               "Mid channel bar"))+
  theme_bw(base_size=18) +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_text(size=14),
    axis.text.x = element_text(size=14),
    legend.position = "bottom" 
  ) +
  labs(y = "Quantity", fill = "")

0 个答案:

没有答案