Skip to content

Instantly share code, notes, and snippets.

@IvoVillanueva
Created June 26, 2022 11:35
Show Gist options
  • Save IvoVillanueva/27e49fa5229b05811fcc9e93299551a3 to your computer and use it in GitHub Desktop.
Save IvoVillanueva/27e49fa5229b05811fcc9e93299551a3 to your computer and use it in GitHub Desktop.
# Librerias ----------------------------------------------------------
library(tidyverse)
library(nbastatR)
library(here)
library(rcartocolor)
library(scales)
library(httr) #para scrapear la tabla de emparejamientos
library(jsonlite) #para reestructurar los datos del Json extraido
# Duplicar el tamaño of the connection buffer siempre lo uso para la libreria nbastatR
Sys.setenv("VROOM_CONNECTION_SIZE" = 131072 * 2)
# establecer tema ---------------------------------------------------------
theme_ivo <- function() {
library(ggtext)
theme_minimal(base_size = 9, base_family = "Inconsolata") %+replace%
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "#fff5e3", color = "#fff5e3"),
plot.caption = element_markdown(
size = 10, family = "Chivo", color = "#626060",
margin = unit(c(.5, 0, 0.5, 0), "cm")
)
)
}
# Data Wrangling ----------------------------------------------------------
# encontrar los logs de los partidos en playoffs
logs <- game_logs(seasons = 2022, season_types = "Playoffs")
# filtrar los partidos entre Boston y Golden State
logs <- logs %>%
filter((slugTeam == "BOS" & slugOpponent == "GSW") | (slugTeam == "GSW" & slugOpponent == "BOS"))
logs <- unique(logs$idGame)#logs para mapear la funcion
# función que extrae las estadisticas de los emparejamientos
matchup <- function(logs) {
headers <- c(
"Accept" = "*/*",
"Accept-Encoding" = "gzip, deflate, br",
"Accept-Language" = "es-ES,es;q=0.9,en;q=0.8",
"Cache-Control" = "no-cache",
"Connection" = "keep-alive",
"Host" = "stats.nba.com",
"Origin" = "https://www.nba.com",
"Pragma" = "no-cache",
"Referer" = "https://www.nba.com/",
"sec-ch-ua" = "Not A;Brand';v='99', 'Chromium';v='102', 'Google Chrome';v='102'",
"sec-ch-ua-mobile" = "?0",
"sec-ch-ua-platform" = "macOS",
"Sec-Fetch-Dest" = "empty",
"Sec-Fetch-Mode" = "cors",
"Sec-Fetch-Site" = "same-site",
"User-Agent" = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36"
)
link <- paste0("https://stats.nba.com/stats/boxscorematchupsv3?GameID=00", logs, "&LeagueID=00&endPeriod=0&endRange=28800&rangeType=0&startPeriod=0&startRange=0")
res1 <- GET(url = link, add_headers(.headers = headers))
json_resp1 <- fromJSON(content(res1, "text"))
p <- pluck(json_resp1, "boxScoreMatchups", "homeTeam", "players") %>%
tibble(value = .) %>%
unnest_wider(value) %>%
unnest_longer(col = matchups) %>%
unnest(matchups, names_sep = "_") %>%
unnest(matchups_statistics, names_sep = "_") %>%
mutate(
gameId = pluck(json_resp1$boxScoreMatchups$gameId), # Generamos columnas que se pierden al desplegar el Jason
teamOfense = pluck(json_resp1$boxScoreMatchups$homeTeam$teamTricode), # Generamos columnas que se pierden al desplegar el Jason
teamDefense = pluck(json_resp1$boxScoreMatchups$awayTeam$teamTricode) # Generamos columnas que se pierden al desplegar el Jason
) %>%
select(gameId, personId:playerSlug, teamOfense, position:jerseyNum, teamDefense, matchups_personId:matchups_statistics_shootingFouls)
p1 <- pluck(json_resp1, "boxScoreMatchups", "awayTeam", "players") %>%
tibble(value = .) %>%
unnest_wider(value) %>%
unnest_longer(col = matchups) %>%
unnest(matchups, names_sep = "_") %>%
unnest(matchups_statistics, names_sep = "_") %>%
mutate(
gameId = pluck(json_resp1$boxScoreMatchups$gameId),
teamOfense = pluck(json_resp1$boxScoreMatchups$awayTeam$teamTricode),#donde en p es Home ahora es al revés
teamDefense = pluck(json_resp1$boxScoreMatchups$homeTeam$teamTricode)
) %>%
select(gameId, personId:playerSlug, teamOfense, position:jerseyNum, teamDefense, matchups_personId:matchups_statistics_shootingFouls)
df <- rbind(p, p1)
return(df)
}
matchup_df <- map_df(logs, matchup)
# Gráficos ----------------------------
# equipo en defensa Boston
df_matchup <- matchup_df %>%
filter(teamDefense == "BOS") %>%
distinct() %>%
mutate(
game_number = group_indices(., gameId)
)
b <- df_matchup %>%
filter(
matchups_nameI %in% c("J. Brown", "A. Horford", "P. Pritchard", "M. Smart", "J. Tatum", "D. White", "R. Williams III", "G. Williams"),
nameI %in% c("A. Wiggins", "D. Green", "K. Looney", "K. Thompson", "S. Curry", "J. Poole", "O. Porter Jr.", "A. Iguodala")
) %>%
ggplot(aes(game_number, fct_reorder(nameI, matchups_statistics_partialPossessions))) +
geom_tile(aes(
fill = matchups_statistics_percentageDefenderTotalTime,
color = "black"
),
size = .5
) +
scale_fill_carto_c(palette = paletteer::paletteer_d("rcartocolor::Mint", #copiado tal cual de Owen Philips
direction = 1
)) +
geom_text(aes(
color = ifelse(matchups_statistics_percentageDefenderTotalTime > .5, "#e0e7eb", "#000000"), #si el cuadrado es muy verde cambiar el texto a blanco
label = percent(accuracy = 1, matchups_statistics_percentageDefenderTotalTime)
)) +
scale_color_identity() +
scale_x_continuous(breaks = seq(1, 6, 1), limits = c(0.5, 6.5), position = "top") +
theme_ivo() +
theme(
strip.placement = "outside",
strip.text.x = element_text(size = "12", hjust = 0.5, color = "#000000"),
axis.text.x = element_text(size = "10", hjust = 0.5, color = "#000000"),
axis.text.y = element_text(size = "10", hjust = 0.5, color = "#1D428A"),
axis.title.x = element_text(size = "18", hjust = 0.5, color = "#007A33", face = "bold", vjust = -4),
axis.title.y = element_text(size = "18", hjust = 0.5, color = "#1D428A", face = "bold"),
plot.title = element_markdown(size = 20, face = "bold", hjust = .5),
plot.subtitle = element_text(
size = 15, face = "bold", hjust = .5,
margin = unit(c(0, 0, 0.5, 0), "cm")
),
legend.position = "none",
plot.margin = margin(10, 25, 25, 10)
) +
labs(
x = "Defensive Player",
y = "Offensive Player",
title = "Porcentaje que los <span style='color:#007A33'>**Celtics**</span> han defendido a los <span style='color:#1D428A'>**Warriors**</span>",
subtitle = "En las finales de la NBA por game number",
caption = "**Datos:** *stats.nba.com* **Gráfico:** *Ivo Villanueva* &bull; <span style='font-family: \"Font Awesome 5 Brands\"'>&#xf099;</span> **@elcheff**"
) +
facet_wrap(~ fct_reorder(matchups_nameI, -matchups_statistics_percentageOffensiveTotalTime), nrow = 2, scales = "free_x", strip.position = "top")
foto <- here("fotos", "matchcelticswarriors")
ggsave(glue::glue("{foto}.png"), b, width = 12, height = 12, dpi = "retina")
# equipo en defensa Golden
df_matchup <- matchup_df %>%
filter(teamDefense == "GSW") %>%
distinct() %>%
mutate(
game_number = group_indices(., gameId)
)
g <- df_matchup %>%
filter(
nameI %in% c("J. Brown", "A. Horford", "P. Pritchard", "M. Smart", "J. Tatum", "D. White", "R. Williams III", "G. Williams"),
matchups_nameI %in% c("A. Wiggins", "D. Green", "K. Looney", "K. Thompson", "S. Curry", "J. Poole", "O. Porter Jr.", "A. Iguodala")
) %>%
ggplot(aes(game_number, fct_reorder(nameI, matchups_statistics_partialPossessions))) +
geom_tile(aes(
fill = matchups_statistics_percentageDefenderTotalTime,
color = "black"
),
size = .5
) +
scale_fill_carto_c(palette = paletteer::paletteer_d( "rcartocolor::Mint",
direction = 1
)) +
geom_text(aes(
color = ifelse(matchups_statistics_percentageDefenderTotalTime > .5, "#e0e7eb", "#000000"),
label = scales::percent(accuracy = 1, matchups_statistics_percentageDefenderTotalTime)
)) +
scale_color_identity() +
scale_x_continuous(breaks = seq(1, 6, 1), limits = c(0.5, 6.5), position = "top") +
theme_ivo() +
theme(
strip.placement = "outside",
strip.text.x = element_text(size = "12", hjust = 0.5, color = "#1D428A"),
axis.text.x = element_text(size = "10", hjust = 0.5, color = "#1D428A"),
axis.text.y = element_text(size = "10", hjust = 0.5, color = "#000000"),
axis.title.x = element_text(size = "18", hjust = 0.5, color = "#1D428A", face = "bold", vjust = -4),
axis.title.y = element_text(size = "18", hjust = 0.5, color = "#007A33", face = "bold"),
plot.title = element_markdown(size = 20, face = "bold", hjust = .5),
plot.subtitle = element_text(
size = 15, face = "bold", hjust = .5,
margin = unit(c(0, 0, 0.5, 0), "cm")
),
legend.position = "none",
plot.margin = margin(10, 25, 25, 10)
) +
labs(
x = "Defensive Player",
y = "Offensive Player",
title = "Porcentaje que los <span style='color:#1D428A'>**Warriors**</span> han defendido a los <span style='color:#007A33'>**Celtics**</span>",
subtitle = "En las finales de la NBA por game number",
caption = "**Datos:** *stats.nba.com* **Gráfico:** *Ivo Villanueva* &bull; <span style='font-family: \"Font Awesome 5 Brands\"'>&#xf099;</span> **@elcheff**"
) +
facet_wrap(~ fct_reorder(matchups_nameI, -matchups_statistics_percentageOffensiveTotalTime), nrow = 2, scales = "free_x", strip.position = "top")
foto <- here("fotos", "matchwarriorsceltics")
ggsave(glue::glue("{foto}.png"), g, width = 12, height = 12, dpi = "retina")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment