将geom_rect()添加到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

我正在尝试找出如何在ymin的时间段内从ymaxcount == 0添加一个半透明矩形。我正在努力找出如何向xmin提供xmaxgeom_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))

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

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

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

enter image description here

,

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

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",size = 10))

translucent-rectangles

,

那几周应该有两排吗?如果是这样,我将与dplyr::summarize()进行汇总。

library(magrittr); library(ggplot2)
breaks_x <- seq(min(test$minDate)," 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,size = 10)) +
  labs(fill="Count is Zero")

translucent

我对这种解释的理解不同于@teunbrand( ie ,每个时间段一个矩形)和@wusel( ie ,该时间段的ymax,而不是样本的ymax) 。如果我们都误会了,请描述如何做。

编辑:如果您希望使用@wusel的方法,则可以跳过最大计算,而应使用Inf,这是ggplot的“转到可见比例尺顶部”的快捷方式。

breaks_x <- seq(min(test$minDate)," 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,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,size = 10)) +
  theme(legend.position = "none")

both bars

本文链接:https://www.f2er.com/3148887.html

大家都在问