首页 > 解决方案 > facet_wrap 包含太多日期

问题描述

我不确定如何制作可重复的数据,但我希望我能充分解释它。

我的数据如下所示:

Station       Date Presence       YrMn
1     EMB 2015-05-24        1 2015-05-01
2     EMB 2015-05-25        1 2015-05-01
3     EMB 2015-05-26        1 2015-05-01
4     EMB 2015-05-27        1 2015-05-01
5     EMB 2015-05-28        1 2015-05-01
6     EMB 2015-05-29        1 2015-05-01

其中 Date 经历 2015-2017 年,Presence 包括 1(存在)、0(缺席)和?(未知)。

我已经成功地将它绘制成这样

ggplot(subset(ALL, Station=="STF"& Presence=="1"), aes(x=YrMn, fill = factor(Presence))) +
  geom_bar() +
  scale_x_date(date_breaks = "1 month", labels = dte_formatter)+
  scale_fill_manual(values=c("#F8766D"), name="Daily Presence", labels=c("Yes") )+
  geom_rect(
    fill = "light grey", alpha = .3, 
    xmin = as.Date("2016-04-19"),
    xmax = as.Date("2016-09-16"),
    ymin = 0,
    ymax = Inf)+
  ylim(0,30)+
  theme_classic()

在此处输入图像描述

但是,一旦我添加 facet_wrap 以包括我得到的年份:

ggplot(subset(ALL, Station=="STF"& Presence=="1"), aes(x=YrMn, fill = factor(Presence))) +
  geom_bar() +
  scale_x_date(date_breaks = "1 month", labels = dte_formatter, expand = c(0,-20))+
  facet_wrap(~year(Date), nrow=1, scales= "fixed")+
  scale_fill_manual(values=c("#F8766D"), name="Daily Presence", labels=c("Yes") )+
  geom_rect(
    fill = "light grey", alpha = .3, 
    xmin = as.Date("2016-04-19"),
    xmax = as.Date("2016-09-16"),
    ymin = 0,
    ymax = Inf)+
  ylim(0,30)+
  theme_classic()+
  theme(strip.placement = "outside",
        strip.background = element_blank(),
        panel.spacing=unit(0.1,"cm"))

在此处输入图像描述

或者

ggplot(subset(ALL, Station=="STF"& Presence=="1"), aes(x=YrMn, fill = factor(Presence))) +
  geom_bar() +
  scale_x_date(date_breaks = "1 month", labels = dte_formatter, expand = c(0,-20))+
  facet_wrap(~year(Date), nrow=1, scales = "free_x")+
  scale_fill_manual(values=c("#F8766D"), name="Daily Presence", labels=c("Yes") )+
  geom_rect(
    fill = "light grey", alpha = .3, 
    xmin = as.Date("2016-04-19"),
    xmax = as.Date("2016-09-16"),
    ymin = 0,
    ymax = Inf)+
  ylim(0,30)+
  theme_classic()+
  theme(strip.placement = "outside",
        strip.background = element_blank(),
        panel.spacing=unit(0.1,"cm"))

在此处输入图像描述

关于如何在保持可靠图表的同时获得年份标签的任何建议?

标签: rggplot2

解决方案


推荐阅读