---
title: "Analysis of Spanish spatial shooting data"
author: "Guillermo Vinue"
date: "`r Sys.Date()`" 
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Analysis of Spanish spatial shooting data}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

This document contains all the needed R code to reproduce the results described in the paper *Adding shooting data to a centralized platform for basketball data visualization*.

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r packages, message=FALSE, eval=FALSE}
# Firstly, load BAwiR:
library(BAwiR) 
```

An example for obtaining the spatial shooting data from the official ACB website is as follows. Define the analyst's name, user agent and x_apikey accordingly.

```{r data, eval=FALSE}
data_days <- do_scrape_days_acb("2024", "analyst_name", TRUE, 2)
 
data_shots <- do_scrape_shots_acb(data_days[1:2, ], TRUE, "user_agent_def", "x_apikey")
```

BAwiR contains a sample of the shooting data from the ACB 2024-2025 regular season. We can see it with these commands:

```{r, eval=FALSE}
?acb_shooting_data_2425
head(acb_shooting_data_2425)
```

Players and teams belonging to the data frame can be obtained with these commands:

```{r, eval=FALSE}
sort(unique(acb_shooting_data_2425$player_name))
sort(unique(acb_shooting_data_2425$full_name))
```

The basketball court is divided into 10 zones as follows:

```{r, eval=FALSE}
df0 <- do_divide_court_zones(acb_shooting_data_2425)
```

Several filters can be applied with the following function:

```{r, eval=FALSE}
df1 <- do_filter_data(df0, "2024-2025", "", "", "", "", "")
```

The violin and boxplots to investigate the data distributions and statistical summaries are created as follows:

```{r, eval=FALSE}
do_violin_box_plots(df1, acb_players_2425)
```

The plot of the shooting patterns of the entire league by zones can be obtained as follows:

```{r, eval=FALSE}
shots_stats <- do_shots_stats(df1, df0) 
 
do_viz_shots_scatter(shots_stats, "all", FALSE)
```

The plots with the location of each individual shot attempted by Dylan Ennis and his shooting patterns by zones can be obtained as follows:

```{r, eval=FALSE}
df1 <- do_filter_data(df0, "2024-2025", "", "", "", "", "D. Ennis")
 
shots_stats <- do_shots_stats(df1, df0)  
 
do_viz_shots_scatter(shots_stats, "player", TRUE)
do_viz_shots_scatter(shots_stats, "player", FALSE)
```

The plots with the comparison of the Ennis' and league field goal percentage and the heatmap showing the distribution of Ennis' shot attempts across the court can be obtained as follows:

```{r, eval=FALSE}
do_viz_shots_gradient(df1, "player", "none", df0)
do_viz_shots_gradient(df1, "player", "fg", df0)
```

Finally, the code to create the stickers is as follows:

```{r, eval=FALSE}
data_player_eff <- data.frame(team = "Real Madrid", 
                              win_perc = "83.3% (5/6)",
                              pts_poss = 114,
                              pts_poss_opp = 104,
                              net_rtg = 10)
 
get_sticker(data_player_eff, acb_sticker_data_2526, "A. Abalde", "English")
```


```{r session info}
sessionInfo()
```