我有一个条形图,它使用x轴的时间序列数据,每个条形图代表7天。
以下是数据框的前几行:
# A tibble: 40 x 5
# Groups: group [26]
group thing minDate count dateRange
<drtn> <dbl> <date> <dbl> <chr>
1 1 days 0 2019-04-02 39 Apr 02 - Apr 08
2 2 days 0 2019-04-09 39 Apr 09 - Apr 15
3 3 days 0 2019-04-16 39 Apr 16 - Apr 22
4 4 days 0 2019-04-23 39 Apr 23 - Apr 29
5 5 days 0 2019-04-30 39 Apr 30 - May 06
我正在尝试找出如何在ymin
的时间段内从ymax
到count == 0
添加一个半透明矩形。我正在努力找出如何向xmin
提供xmax
和geom_rect()
值。有人知道我该怎么做吗?
来自dput()
的完整数据:
test <-
structure(list(group = structure(c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,26),class = "difftime",units = "days"),thing = c(0,0.0476190476190476,0.325396825396825,0.388888888888889,0.214285714285714,0.103174603174603,0.158730158730159,0.0555555555555556,0.0512820512820513,0.0769230769230769,0.205128205128205,0.102564102564103,0.58974358974359,0.615384615384615,0.358974358974359,0.41025641025641,0.256410256410256,0.230769230769231,0.130904183535762,0),minDate = structure(c(17988,17995,18002,18009,18016,18023,18030,18037,18044,18051,18058,18065,18072,18079,18086,18093,18100,18107,18114,18121,18128,18135,18142,18149,18156,18163,18163),class = "Date"),count = c(39,39,51,27,108,78,117,dateRange = c("Apr 02 - Apr 08","Apr 09 - Apr 15","Apr 16 - Apr 22","Apr 23 - Apr 29","Apr 30 - May 06","May 07 - May 13","May 14 - May 20","May 21 - May 27","May 28 - Jun 03","Jun 04 - Jun 10","Jun 11 - Jun 17","Jun 18 - Jun 24","Jun 25 - Jul 01","Jul 02 - Jul 08","Jul 09 - Jul 15","Jul 16 - Jul 22","Jul 23 - Jul 29","Jul 30 - Aug 05","Aug 06 - Aug 12","Aug 13 - Aug 19","Aug 20 - Aug 26","Aug 27 - Sep 02","Sep 03 - Sep 09","Sep 10 - Sep 16","Sep 17 - Sep 23","Sep 24 - Sep 30","Sep 24 - Sep 30")),row.names = c(NA,-40L),class = c("grouped_df","tbl_df","tbl","data.frame"),groups = structure(list(
group = structure(c(1,.rows = list(1L,2L,3L,4L,5L,6L,7L,8L,9L,10L,11L,12L,13:14,15:16,17:18,19:20,21:22,23:24,25:26,27:28,29:30,31:32,33:34,35:36,37:38,39:40)),-26L),class = c("tbl_df",.drop = TRUE))
工作图:
library(ggplot2); library(magrittr)
test %>%
ggplot(aes(x = minDate,y = thing)) +
geom_bar(stat = "identity") +
scale_x_date(breaks = seq(min(test$minDate),max(test$minDate),by = paste0(7," days")),date_labels = unique(test$dateRange)) +
theme(axis.text.x = element_text(angle = 90,hjust = 1,size = 10))