# Split the data by year and create separate plots
year_plots <- sb %>%
group_split(year) %>%
map(~{
year_val <- unique(.x$year)
ggplot(.x, aes(x = hours_to_game_begin, y = avg_leq,
colour = super_bowl_sunday, linetype = super_bowl_sunday)) +
geom_line() +
facet_grid(. ~ game_zone) +
ggtitle(paste("Super Bowl Noise -", year_val)) +
theme_minimal() +
easy_remove_gridlines(axis = c("x")) +
scale_colour_manual(values = c("black", "red")) +
scale_linetype_manual(values = c("dashed", "solid")) +
easy_remove_legend() +
annotate("rect", xmin = 0, xmax = 3.5, ymin = -Inf, ymax = Inf, alpha = 0.1, fill = "darkgrey") +
scale_x_continuous(limits = c(-6, 9), breaks= seq(-6, 9, 3)) +
scale_y_continuous(breaks= seq(50, 70, 5)) +
labs( x = "Hours from start of Super Bowl", y = "Average noise exposure (in decibels)", caption = "Data from Apple Hearing Study") +
ggtitle(paste("Super Bowl Noise -", year_val)) +
geom_text(data = data.frame(x = -1.3, y = 53, label = "Start game \n6:30 PM EST", game_zone = "Game States", year = "2024"),
mapping = aes(x = x, y = y, label = label),
size = 2, inherit.aes = FALSE) +
geom_text(data = data.frame(x = 2.3, y = 53, label = "End game \n10:00 PM EST", game_zone = "Game States", year = "2024"),
mapping = aes(x = x, y = y, label = label),
size = 2, inherit.aes = FALSE) +
geom_text(data = data.frame(x = -1.3, y = 53, label = "Start game \n6:30 PM EST", game_zone = "Non-game States", year = "2024"),
mapping = aes(x = x, y = y, label = label),
size = 2, inherit.aes = FALSE) +
geom_text(data = data.frame(x = 2.3, y = 53, label = "End game \n10:00 PM EST", game_zone = "Non-game States", year = "2024"),
mapping = aes(x = x, y = y, label = label),
size = 2, inherit.aes = FALSE) +
theme(panel.spacing = unit(1, "lines"))
})
# save all plots
walk2(
year_plots,
unique(sb$year),
~ggsave(filename = paste0("superbowl_noise_", .y, ".png"),
plot = .x, width = 10, height = 6)
)