如何用每日数据绘制多个重叠的 18 个月期间的图表?
How can you graph multiple overlapping 18 month periods with daily data?
我正在对多年来每天收集的数据进行探索性数据分析。相关时间段为每年同日起计约18-20个月。我想做的是逐个目视检查这 18 个月的时间段。我可以通过为每个 geom_point() 调用添加数据来执行此操作,如下所示。我想避免在每个时期调用一次
分钟前:
library(tidyverse)
minex <- data.frame(dts = seq((mdy('01/01/2010')), mdy('11/10/2013'), by = 'days'))
minex$day <- as.numeric(minex$dts - min(minex$dts))
minex$MMDD <- paste0(month(minex$dts), "-", day(minex$dts))
minex$v1 <- 20 + minex$day^0.4 -cos(2*pi*minex$day/365) + rnorm(nrow(minex), 0, 0.3)
ggplot(filter(minex, dts %in% seq((mdy('11/10/2013') - (365 + 180)), mdy('11/10/2013'), by =
'days')), aes(day, v1)) +
geom_point() +
geom_point(data = filter(minex, dts %in% seq((mdy('11/10/2012') - (365 + 180)),
mdy('11/10/2012'), by = 'days')), aes(day+365, v1), color = 'red')
因为你有重叠的时间跨度,我想我们可以 lapply
在你的结束日期,稍微改变数据,然后使用正常的 ggplot2
美学来给它们着色。
spans <- bind_rows(lapply(mdy("11/10/2010", "11/10/2011", "11/10/2012", "11/10/2013"), function(end) {
filter(minex, between(dts, end - (365 + 180), end)) %>%
mutate(day = day - min(day), end = end)
}))
ggplot(spans, aes(day, v1)) +
geom_point(aes(color = factor(end)))
您可以通过快速摘要查看每个的范围:
spans %>%
group_by(end) %>%
summarize(startdate = min(dts), enddate = max(dts))
# # A tibble: 4 x 3
# end startdate enddate
# <date> <date> <date>
# 1 2010-11-10 2010-01-01 2010-11-10
# 2 2011-11-10 2010-05-14 2011-11-10
# 3 2012-11-10 2011-05-15 2012-11-10
# 4 2013-11-10 2012-05-14 2013-11-10
我正在对多年来每天收集的数据进行探索性数据分析。相关时间段为每年同日起计约18-20个月。我想做的是逐个目视检查这 18 个月的时间段。我可以通过为每个 geom_point() 调用添加数据来执行此操作,如下所示。我想避免在每个时期调用一次
分钟前:
library(tidyverse)
minex <- data.frame(dts = seq((mdy('01/01/2010')), mdy('11/10/2013'), by = 'days'))
minex$day <- as.numeric(minex$dts - min(minex$dts))
minex$MMDD <- paste0(month(minex$dts), "-", day(minex$dts))
minex$v1 <- 20 + minex$day^0.4 -cos(2*pi*minex$day/365) + rnorm(nrow(minex), 0, 0.3)
ggplot(filter(minex, dts %in% seq((mdy('11/10/2013') - (365 + 180)), mdy('11/10/2013'), by =
'days')), aes(day, v1)) +
geom_point() +
geom_point(data = filter(minex, dts %in% seq((mdy('11/10/2012') - (365 + 180)),
mdy('11/10/2012'), by = 'days')), aes(day+365, v1), color = 'red')
因为你有重叠的时间跨度,我想我们可以 lapply
在你的结束日期,稍微改变数据,然后使用正常的 ggplot2
美学来给它们着色。
spans <- bind_rows(lapply(mdy("11/10/2010", "11/10/2011", "11/10/2012", "11/10/2013"), function(end) {
filter(minex, between(dts, end - (365 + 180), end)) %>%
mutate(day = day - min(day), end = end)
}))
ggplot(spans, aes(day, v1)) +
geom_point(aes(color = factor(end)))
您可以通过快速摘要查看每个的范围:
spans %>%
group_by(end) %>%
summarize(startdate = min(dts), enddate = max(dts))
# # A tibble: 4 x 3
# end startdate enddate
# <date> <date> <date>
# 1 2010-11-10 2010-01-01 2010-11-10
# 2 2011-11-10 2010-05-14 2011-11-10
# 3 2012-11-10 2011-05-15 2012-11-10
# 4 2013-11-10 2012-05-14 2013-11-10