Skip to content

Instantly share code, notes, and snippets.

@gallochris
Last active January 22, 2024 15:16
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 gallochris/c9bd308e470c00e5c13f1288d1dbf5b3 to your computer and use it in GitHub Desktop.
Save gallochris/c9bd308e470c00e5c13f1288d1dbf5b3 to your computer and use it in GitHub Desktop.
Defensive efficiency plot
# Attempt to recreate or riff on the plot from the Tufte book
# https://opticalacumen.blogspot.com/2014/07/data-visualization-in-1919.html
# Fetch the player minutes data by game over past three seasons
hubert <-
cbbdata::cbd_torvik_game_factors(team = "North Carolina") |>
dplyr::filter(year > 2021) |>
dplyr::arrange(date) |>
dplyr::mutate(off_ppp = format(round(off_ppp / 100, 2), nsmall = 2),
def_ppp = format(round(def_ppp / 100, 2), nsmall = 2)) |>
dplyr::select(game_id, date, off_ppp, opp, result, def_ppp)
# Add game numbers by month now
def_by_month <-
hubert |>
dplyr::mutate(month_year = format(as.Date(date), "%B %Y")) |>
dplyr::arrange(date) |>
dplyr::group_by(month_year) |>
dplyr::mutate(game_num = dplyr::row_number()) |>
dplyr::select(game_id,
date,
month_year,
game_num,
off_ppp,
opp,
result,
def_ppp)
# Set custom order
dates_order <- c(
"November 2021",
"December 2021",
"January 2022",
"February 2022",
"March 2022",
"April 2022",
"November 2022",
"December 2022",
"January 2023",
"February 2023",
"March 2023",
"November 2023",
"December 2023",
"January 2024"
)
# Set your "month_year" labels as factors with the desired order
def_by_month$date_label <-
factor(def_by_month$month_year, levels = dates_order)
# Make the plot using hrbrthemes
def_plot <- def_by_month |>
dplyr::arrange(date) |>
ggplot2::ggplot(ggplot2::aes(x = date_label, y = game_num)) +
ggplot2::geom_text(
data = def_by_month |> dplyr::filter(def_ppp >= 1),
ggplot2::aes(label = def_ppp),
size = 3.5,
color = "#3182bd",
family = "Roboto Condensed",
) +
ggplot2::geom_text(
data = def_by_month |> dplyr::filter(def_ppp < 1),
ggplot2::aes(label = def_ppp),
size = 3.5,
color = "#deebf7",
family = "Roboto Condensed",
fontface = "bold"
) +
ggplot2::scale_y_continuous(limits = c(1, 9), breaks = seq(1, 9, 1)) +
ggplot2::scale_x_discrete(labels = scales::label_wrap(5)) +
hrbrthemes::theme_modern_rc() +
ggplot2::geom_vline(
xintercept = c(6.5, 11.5),
linetype = "dashed",
color = "#acacac"
) +
ggplot2::annotate(
"label",
x = 1.5,
y = 8.5,
label = "2021-22 Season \n20 of 39 opponents \nunder 1 PPP",
family = "Roboto Condensed",
size = 3.5,
color = "#333333",
fontface = "bold",
fill = "#deebf7"
) +
ggplot2::annotate(
"label",
x = 8.5,
y = 8.5,
label = "2022-23 Season \n16 of 33 opponents \nunder 1 PPP",
family = "Roboto Condensed",
size = 3.5,
color = "#333333",
fontface = "bold",
fill = "#deebf7"
) +
ggplot2::annotate(
"label",
x = 13,
y = 8.5,
label = "2023-24 Season \n12 of 18 opponents \nunder 1 PPP",
family = "Roboto Condensed",
size = 3.5,
color = "#333333",
fontface = "bold",
fill = "#deebf7"
) +
hrbrthemes::theme_modern_rc() +
ggplot2::theme(
axis.text.y = ggplot2::element_blank(),
panel.grid = ggplot2::element_blank(),
) +
ggplot2::labs(
x = "",
y = "",
title = "North Carolina Basketball: Defensive efficiency by month over past three seasons",
subtitle = "Shows opponent points per possession by game for each month over past three seasons",
caption = "Bless your chart | January 21, 2024 | data via cbbdata"
)
# Save the plot
ggplot2::ggsave(
"def_plot.png",
def_plot,
w = 12,
h = 8,
dpi = 600,
type = 'cairo'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment