r - 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"))
关于如何在保持可靠图表的同时获得年份标签的任何建议?
解决方案
推荐阅读
- reactjs - Reactjs条件渲染问题
- html - div 内的视频背景和文本不会随着页面的其余部分滚动?
- flutter - 将 camera_platform_interface 从 1.0.4 升级到 1.2.0 导致相机无法工作
- sql - SELECT 语句中的表达式
- python - python if 语句无法与 gensim 语言模型一起正常工作
- php - WordPress Woocommerce Limited by PHP-FPM Processes Ram Utilization 的最大用户数
- python - 中断线程中的无限循环
- python - 请求的 URL 不允许该方法:使用 Flask POST 方法
- javascript - 在功能组件中使用方法回调测试按钮
- reactjs - 使用 React 的 UseEffect 和事件监听器的区别?