adj_data <- unc_factors |>
dplyr::arrange(date) |>
dplyr::mutate(
adj_o_roll = zoo::rollmean(adj_o, k = 5, fill = NA, align = 'center'),
adj_d_roll = zoo::rollmean(adj_d, k = 5, fill = NA, align = 'center')
) |>
tidyr::pivot_longer(
cols = c(adj_o, adj_d, adj_o_roll, adj_d_roll),
names_to = "metric",
values_to = "value"
) |>
dplyr::mutate(
is_rolling = stringr::str_detect(metric, "_roll$"),
base_metric = stringr::str_remove(metric, "_roll$"),
team_type = dplyr::case_when(
stringr::str_detect(base_metric, "adj_o") ~ "Offense",
stringr::str_detect(base_metric, "adj_d") ~ "Defense"
)
) |>
# Remove NA values for the rolling averages
dplyr::filter(!(is_rolling & is.na(value)))
# Create rebound plot
ggplot2::ggplot(adj_data,
ggplot2::aes(x = date, y = value,
color = team_type, fill = team_type)) +
ggplot2::geom_point(
data = ~ dplyr::filter(., !is_rolling),
alpha = 0.3,
size = 1
) +
ggplot2::stat_smooth(
data = ~ dplyr::filter(., is_rolling),
method = "loess",
span = 0.70,
alpha = 0.2,
se = TRUE
) +
ggthemes::theme_fivethirtyeight() +
ggplot2::labs(
title = "North Carolina: Adjusted Efficiency Since 2017-18 Season",
subtitle = "Shows <span style='color:#56a0d3;'>offensive</span> and <span style='color:red;'>defensive</span> adjusted efficiency over a five-game rolling average since start of the 2017-18 season.",
caption = "Data via barttorvik.com | Viz by Chris at Bless your Chart"
) +
ggplot2::theme(
plot.subtitle = ggtext::element_markdown(),
legend.position = "none",
plot.caption = ggtext::element_markdown(size = 8)
) +
ggplot2::scale_color_manual(
values = c("Offense" = "#56a0d3", "Defense" = "red")
) +
ggplot2::scale_fill_manual(
values = c("Offense" = "#56a0d3", "Defense" = "red")
) +
ggplot2::scale_x_date(
breaks = custom_breaks,
labels = custom_labels,
limits = as.Date(c("2017-11-01", "2025-01-30"))
) +
ggplot2::geom_vline(xintercept = as.Date("2021-10-31"), linetype = "dashed",
color = "#868686") +
ggplot2::scale_y_continuous(breaks = seq(60, 160, 20),
limits = c(60, 160)
) +
ggplot2::annotate(
geom = "label",
x = as.Date("2019-06-25"),
y = 150,
color = "#868686",
fill = "floralwhite",
label = "Roy Williams \nAverages \n113.8 (offense) \n94.4 (defense)",
size = 3,
family = 'mono',
fontface = 'bold'
) +
ggplot2::annotate(
geom = "label",
x = as.Date("2023-06-25"),
y = 150,
color = "#868686",
fill = "floralwhite",
label = "Hubert Davis \nAverages \n114.8 (offense) \n95.2 (defense)",
size = 3,
family = 'mono',
fontface = 'bold'
) -> adj_plot
ggplot2::ggsave(
"adj_plot.png",
adj_plot,
w = 10,
h = 7.5,
dpi = 600,
bg = "white",
type = 'cairo'
)
adj_plot