Skip to content

Instantly share code, notes, and snippets.

@jonspring
Created October 21, 2022 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonspring/1bb786b17f017fda2394ec36b5b6832b to your computer and use it in GitHub Desktop.
Save jonspring/1bb786b17f017fda2394ec36b5b6832b to your computer and use it in GitHub Desktop.
library(tidyverse)
library(readxl)
library(lubridate)
library(ggdist)
historicalweeklydata <-
bind_rows(
read_excel("~/Downloads/historicalweeklydata.xls",
sheet = "Full History", range = "a7:B4000"),
read_excel("~/Downloads/historicalweeklydata.xls",
sheet = "1PMMS2022", range = "A7:B100")
)
historicalweeklydata %>%
uncount(60*12, .id = "offset_mo") %>%
mutate(offset_mo = offset_mo - 1) %>%
mutate(Week_adj = as_date(Week + 60*60*24*30.5*(offset_mo))) %>%
filter(Week_adj >= ymd(20220101),
Week_adj <= ymd(20240101)) %>%
group_by(offset_mo) %>%
mutate(FRM_chg = FRM - first(FRM)) %>%
ungroup() -> temp
temp %>%
filter(Week_adj >= max(Week_adj[offset_mo == 0] - 6),
Week_adj <= max(Week_adj[offset_mo == 0])) %>%
mutate(var = FRM_chg - FRM_chg[offset_mo == 0]) %>%
filter(abs(var) < 0.5, abs(var) != 0) %>%
mutate(starts = Sys.Date() - 30.5*offset_mo)
select(offset_mo) %>%
left_join(temp) %>%
-> similar
ggplot(temp, aes(Week_adj, FRM_chg)) +
geom_line(aes(group = offset_mo), alpha = 0.2, color = "gray40") +
stat_lineribbon(.width = ppoints(90), alpha = 0.02, color = NA) +
geomtextpath::geom_textline(data = similar, color = "darkgoldenrod4",
aes(group = offset_mo, label = starts), hjust = 0.6,
text_smoothing = 20, size = 2) +
geom_line(data = . %>% filter(offset_mo == 0), size = 1.2) +
theme_minimal() +
guides(fill = "none")
ggsave("FRM distribution.png", width=7, height=4, dpi=300,
scale = 1.2, bg = "white")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment