【发布时间】:2020-03-23 01:32:36
【问题描述】:
首先,让我们创建一些假数据:
d <- c("2019-01-01", "2019-01-02", "2019-01-03", "2019-01-04", "2019-01-03", "2019-04-06", "2019-04-03", "2019-05-07", "2019-05-03", "2019-05-03", "2019-05-03", "2019-05-03", "2019-06-03", "2019-06-03", "2019-06-03", "2019-06-03", "2019-06-03", "2019-06-03", "2019-06-03", "2019-07-03", "2019-07-03", "2019-07-04", "2019-08-03", "2019-09-05", "2019-09-03", "2019-09-03", "2019-09-06", "2019-09-08", "2019-10-03", "2019-11-03", "2019-11-03", "2019-11-03", "2019-11-03", "2019-11-03", "2019-11-03", "2019-12-03", "2019-12-03")
df <- data.frame(dates=as.Date(d))
现在,我创建一个时间序列图:
# aggregate data
df_plot <- df %>% mutate(month = lubridate::floor_date(dates, "month")) %>%
group_by(month) %>% summarise(count = n())
# plot data
ggplot(aes(x = month, y = count), data = df_plot) + geom_line() +
scale_x_date() +
geom_vline(xintercept = as.numeric(as.Date("2019-01-30")), linetype=4)
使用geom_vline(xintercept = as.numeric(as.Date("2019-01-30")), linetype=4),我可以用垂直线标记某个日期。是否还有可能用彩色框或其他东西标记时间-范围(比如说从 2019 年 1 月 30 日到 2019 年 2 月 15 日)?
【问题讨论】:
-
sthda.com/english/wiki/… - 查找
geom_ribbon,有一个geom_rect的例子
标签: r ggplot2 time-series geom-vline