monthly_visits <- visits %>%
mutate(
dt1 = floor_date(visitStartTime, unit = "month"),
year = year(dt1)) %>%
group_by(dt1, year) %>%
summarise(
number_of_visits_per_month = n(),
.groups = "drop"
) %>%
arrange(dt1)
yearly_visits_roll <- monthly_visits %>%
mutate(
nrvisits = rollsum(
number_of_visits_per_month,
k = 12,
align = "right",
fill = NA))
ggplotly( ggplot(yearly_visits_roll, aes(x = dt1, y = nrvisits)) +
geom_point(aes(color = factor(year)), size = 1) +
geom_line(aes(color = factor(year))) +
labs(
title = "Rolling 12-month number of visits",
x = "Month",
y = "Number of visits (12-month sum)",
color = "Year"))