[英]add geom_rect() to time series data in ggplot2
我有一个条形图,它使用 x 轴的时间序列数据,其中每个条形代表 7 天。
这是 dataframe 的前几行:
# 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
的半透明矩形。 我正在努力弄清楚如何将xmin
和xmax
值提供给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))
以下似乎产生了我认为你的目标。 请注意, 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))
我不确定您想要的 output 应该是什么样子,但这应该只需要您自己进行一些调整:
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))
那几周应该有两行吗? 如果是这样,我会与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(即每个时间段一个矩形)和@wusel(即时间段的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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.