将 geom_rect() 添加到 ggplot2 中的时间序列数据

use*_*170 2 r time-series ggplot2

我有一个条形图,它使用 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
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚如何在 的时间段中添加一个半透明矩形从ymin到。我正在努力弄清楚如何提供和价值观ymaxcount == 0xminxmaxgeom_rect()。有谁知道我如何实现这个目标?

完整数据来自dput()

    test <- 
structure(list(group = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 
                                   10, 11, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 
                                   19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26), class = "difftime", units = "days"), 
               thing = c(0, 0, 0, 0, 0, 0.0476190476190476, 0.325396825396825, 
                         0.388888888888889, 0.214285714285714, 0.103174603174603, 
                         0.158730158730159, 0.0555555555555556, 0, 0, 0.0512820512820513, 
                         0, 0.0769230769230769, 0, 0.205128205128205, 0, 0.102564102564103, 
                         0, 0.58974358974359, 0, 0.615384615384615, 0, 0.358974358974359, 
                         0, 0.0769230769230769, 0, 0.41025641025641, 0, 0.256410256410256, 
                         0, 0.230769230769231, 0, 0, 0, 0.130904183535762, 0), minDate = structure(c(17988, 
                                                                                                     17995, 18002, 18009, 18016, 18023, 18030, 18037, 18044, 18051, 
                                                                                                     18058, 18065, 18072, 18072, 18079, 18079, 18086, 18086, 18093, 
                                                                                                     18093, 18100, 18100, 18107, 18107, 18114, 18114, 18121, 18121, 
                                                                                                     18128, 18128, 18135, 18135, 18142, 18142, 18149, 18149, 18156, 
                                                                                                     18156, 18163, 18163), class = "Date"), count = c(39, 39, 
                                                                                                                                                      39, 39, 39, 39, 51, 27, 39, 39, 39, 39, 9, 39, 108, 0, 78, 
                                                                                                                                                      0, 78, 0, 39, 0, 117, 0, 78, 0, 78, 0, 39, 0, 117, 0, 78, 
                                                                                                                                                      0, 78, 0, 78, 0, 78, 0), 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", 
                                                                                                                                                                                             "Jun 25 - Jul 01", "Jul 02 - Jul 08", "Jul 02 - Jul 08", 
                                                                                                                                                                                             "Jul 09 - Jul 15", "Jul 09 - Jul 15", "Jul 16 - Jul 22", 
                                                                                                                                                                                             "Jul 16 - Jul 22", "Jul 23 - Jul 29", "Jul 23 - Jul 29", 
                                                                                                                                                                                             "Jul 30 - Aug 05", "Jul 30 - Aug 05", "Aug 06 - Aug 12", 
                                                                                                                                                                                             "Aug 06 - Aug 12", "Aug 13 - Aug 19", "Aug 13 - Aug 19", 
                                                                                                                                                                                             "Aug 20 - Aug 26", "Aug 20 - Aug 26", "Aug 27 - Sep 02", 
                                                                                                                                                                                             "Aug 27 - Sep 02", "Sep 03 - Sep 09", "Sep 03 - Sep 09", 
                                                                                                                                                                                             "Sep 10 - Sep 16", "Sep 10 - Sep 16", "Sep 17 - Sep 23", 
                                                                                                                                                                                             "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, 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), class = "difftime", units = "days"), 
                                                                                                                                                                                                                                                                        .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)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                      -26L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))  
Run Code Online (Sandbox Code Playgroud)

工作图:

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))
Run Code Online (Sandbox Code Playgroud)

酒吧

teu*_*and 5

以下似乎大致得出了我认为您的目标。请注意,这geom_tile()非常相似geom_rect(),但参数化为 x/y/width/height,而不是 xmin/xmax/ymin/ymax。因此,我们可以为每个 制作一系列瓷砖count == 0

ggplot(test, aes(x = minDate, 
                 y = thing)) +
  geom_tile(data = test[test$count == 0, ],
            aes(x = minDate, 
                y = 0.5 * max(test$thing), 
                # A width of 7 days
                width = 7, 
                height = max(test$thing)), 
            alpha = 0.2) +
  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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述