【问题标题】:add geom_rect() to time series data in ggplot2将 geom_rect() 添加到 ggplot2 中的时间序列数据
【发布时间】:2020-03-03 10:30:26
【问题描述】:

我有一个条形图,它使用 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

我试图弄清楚如何在count == 0 的时间段内将一个半透明矩形从ymin 添加到ymax。我正在努力弄清楚如何将xminxmax 值提供给geom_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))  

工作图:

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))

【问题讨论】:

    标签: r ggplot2 time-series


    【解决方案1】:

    以下内容似乎大致达到了我认为您的目标。请注意,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))
    

    【讨论】:

    • 从 geom_tile() 映射宽度参数正是我想要的。没想到要使用这个几何图层。
    【解决方案2】:

    那几周应该有两行吗?如果是这样,我会与dplyr::summarize() 聚合。

    library(magrittr); library(ggplot2)
    breaks_x <- seq(min(test$minDate), max(test$minDate), by = paste0(7, " days"))
    palette_translucent <- c("TRUE" = "#77777744", "FALSE"="#777777")
    
    ds <- 
      test %>% 
      dplyr::mutate(
        translucent = (abs(count)  < .0000001)
      ) %>% 
      dplyr::group_by(minDate) %>% 
      dplyr::summarize(
        translucent = any(translucent),
        y = max(thing)
      ) %>% 
      dplyr::ungroup()
    
    labels_x  <- unique(test$dateRange)
    ds %>%
      ggplot(aes(x = minDate, y = y, fill = translucent)) +
      geom_bar(stat = "identity") +
      scale_x_date(breaks = breaks_x, date_labels = labels_x) +
      scale_fill_manual(values = palette_translucent) + 
      theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 10)) +
      labs(fill="Count is Zero")
    

    我对此的解释不同于@teunbrand(ie,每个时间段一个矩形)和@wusel(ie,时间段的 ymax,而不是样本) .如果大家都误会了,请描述一下。

    编辑:如果你想要@wusel 的方法,可以跳过最大值计算,而使用Inf,这是ggplot 的“转到可见刻度的顶部”的快捷方式。

    breaks_x <- seq(min(test$minDate), max(test$minDate), by = paste0(7, " days"))
    labels_x  <- unique(test$dateRange)
    palette_translucent <- c("TRUE" = "#77777744", "FALSE"="#777777")
    ds <- 
      test %>% 
      dplyr::mutate(
        translucent = (abs(count)  < .0000001),
        y     = dplyr::if_else(translucent, Inf, thing) # Inf is ggplot's shortcut for "top of the visible scale"
      ) %>% 
      dplyr::ungroup()
    
    ds %>%
      ggplot(aes(x = minDate, y = y, fill = translucent)) +
      geom_bar(aes(y=y), stat = "identity", position=position_identity()) +
      geom_bar(aes(y=thing), stat = "identity") +
      scale_x_date(breaks = breaks_x, date_labels = labels_x) +
      scale_fill_manual(values = palette_translucent) +
      theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 10)) +
      theme(legend.position = "none")
    

    【讨论】:

      【解决方案3】:

      我不太确定您想要的输出应该是什么样子,但这应该只需要您自己进行一些调整:

      library(lubridate)
      library(dplyr)
      
      recs <- test %>% 
        filter(count == 0) %>% 
        mutate(xmin = minDate - days(3),
               xmax = minDate + days(3))
      
      test %>%
        ggplot(aes(x = minDate, 
                   y = thing)) +
        # rectangles are behind the bars
        geom_rect(data = recs, 
                  # some fixed height of the rectangles
                  aes(xmin = xmin, xmax = xmax, ymin = 0, ymax = .7),
                  fill = "red", color = "white", 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))
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-16
        • 2015-11-08
        • 1970-01-01
        • 2019-09-10
        • 1970-01-01
        • 2017-04-10
        • 2022-01-21
        • 2021-08-11
        相关资源
        最近更新 更多