Last active
January 1, 2020 10:45
-
-
Save Ryo-N7/20d5d024349016da0e9fc9c58d4e5878 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pkgs | |
```{r, message=FALSE, warning=FALSE} | |
pacman::p_load(tidyverse, polite, scales, ggimage, | |
ggforce, ggtext, | |
rvest, glue, extrafont, ggrepel, magick) | |
loadfonts() | |
``` | |
## add_logo | |
```{r} | |
add_logo <- function(plot_path, logo_path, logo_position, logo_scale = 10){ | |
# Requires magick R Package https://github.com/ropensci/magick | |
# Useful error message for logo position | |
if (!logo_position %in% c("top right", "top left", "bottom right", "bottom left")) { | |
stop("Error Message: Uh oh! Logo Position not recognized\n Try: logo_positon = 'top left', 'top right', 'bottom left', or 'bottom right'") | |
} | |
# read in raw images | |
plot <- magick::image_read(plot_path) | |
logo_raw <- magick::image_read(logo_path) | |
# get dimensions of plot for scaling | |
plot_height <- magick::image_info(plot)$height | |
plot_width <- magick::image_info(plot)$width | |
# default scale to 1/10th width of plot | |
# Can change with logo_scale | |
logo <- magick::image_scale(logo_raw, as.character(plot_width/logo_scale)) | |
# Get width of logo | |
logo_width <- magick::image_info(logo)$width | |
logo_height <- magick::image_info(logo)$height | |
# Set position of logo | |
# Position starts at 0,0 at top left | |
# Using 0.01 for 1% - aesthetic padding | |
if (logo_position == "top right") { | |
x_pos = plot_width - logo_width - 0.01 * plot_width | |
y_pos = 0.01 * plot_height | |
} else if (logo_position == "top left") { | |
x_pos = 0.01 * plot_width | |
y_pos = 0.01 * plot_height | |
} else if (logo_position == "bottom right") { | |
x_pos = plot_width - logo_width - 0.01 * plot_width | |
y_pos = plot_height - logo_height - 0.01 * plot_height | |
} else if (logo_position == "bottom left") { | |
x_pos = 0.01 * plot_width | |
y_pos = plot_height - logo_height - 0.01 * plot_height | |
} | |
# Compose the actual overlay | |
magick::image_composite(plot, logo, offset = paste0("+", x_pos, "+", y_pos)) | |
} | |
``` | |
# Premier League data | |
## fbref | |
- https://fbref.com/en/comps/9/stats/Premier-League-Stats | |
- Save "Player Standard Stats" & "Squad Standard Stats" as a `.csv` file or whatever you prefer. | |
### squad stats | |
```{r} | |
epl_squad_stats_MD20_raw <- read_csv(here::here("data/epl_squad_stats_MD20.csv"), | |
skip = 1, col_names = TRUE) | |
epl_squad_stats_MD20 <- epl_squad_stats_MD20_raw %>% | |
rename_at(vars(12:16), | |
~ glue::glue("{colnames(epl_squad_stats_MD20_raw)[12:16]}_p90")) %>% | |
rename_at(vars(20:24), | |
~ glue::glue("{colnames(epl_squad_stats_MD20_raw)[20:24]}_p90")) | |
epl_squad_stats_MD20 <- epl_squad_stats_MD20 %>% | |
rename_at(vars(2:24), ~ glue::glue("{colnames(epl_squad_stats_MD20)[2:24]}_squad")) %>% | |
rename_at(vars(contains("_1")), ~ str_replace(., "_1", "")) | |
glimpse(epl_squad_stats_MD20) | |
``` | |
```{r} | |
## save | |
saveRDS(epl_squad_stats_MD20, file = glue("{here::here()}/data/epl_squad_stats_MD20.RDS")) | |
epl_squad_stats_MD20 <- readRDS(file = glue("{here::here()}/data/epl_squad_stats_MD20.RDS")) | |
``` | |
### player stats | |
```{r} | |
epl_player_stats_MD20_raw <- read_csv(here::here("data/epl_player_stats_MD20.csv"), | |
skip = 1, col_names = TRUE) | |
epl_player_stats_MD20 <- epl_player_stats_MD20_raw %>% | |
rename_at(vars(17:21), | |
~ glue::glue("{colnames(epl_player_stats_MD20_raw)[17:21]}_p90")) %>% | |
rename_at(vars(25:29), | |
~ glue::glue("{colnames(epl_player_stats_MD20_raw)[25:29]}_p90")) | |
epl_player_stats_MD20 <- epl_player_stats_MD20 %>% | |
rename_at(vars(contains("_1")), ~ str_replace(., "_1", "")) %>% | |
select(-Matches, -Rk) | |
glimpse(epl_player_stats_MD20) | |
``` | |
```{r} | |
## save | |
saveRDS(epl_player_stats_MD20, file = glue("{here::here()}/data/epl_player_stats_MD20.RDS")) | |
epl_player_stats_MD20 <- readRDS(file = glue("{here::here()}/data/epl_player_stats_MD20.RDS")) | |
``` | |
## clean | |
```{r} | |
epl_player_stats_MD20 %>% | |
left_join(epl_squad_stats_MD20, by = "Squad") %>% | |
glimpse() | |
``` | |
```{r} | |
goal_contribution_clean_df <- epl_player_stats_MD20 %>% | |
left_join(epl_squad_stats_MD20, by = "Squad") %>% | |
separate(Player, into = c("fullname", "allname"), sep = "\\\\") %>% | |
separate(fullname, into = c("firstname", "lastname"), | |
sep = "\\s", extra = "merge", | |
remove = FALSE) %>% | |
mutate(fname = str_extract(firstname, "[A-Z]{1}")) %>% | |
## players like Fabinho listed without Tavares last name | |
mutate(player = if_else(is.na(lastname), firstname, lastname)) %>% | |
mutate(player = case_when( | |
!is.na(lastname) ~ glue("{fname}. {lastname}"), | |
TRUE ~ firstname)) %>% | |
group_by(fullname) %>% | |
mutate(goal_contrib = Gls / Gls_squad, | |
assist_contrib = Ast / Gls_squad) %>% | |
ungroup() %>% | |
## keep fullname so can differentiate A. Gomes (Andre/Angel), etc. | |
select(player, fullname, Pos, Squad, Min, | |
Gls, Gls_squad, PK, PK_squad, | |
Ast, Ast_squad, | |
goal_contrib, assist_contrib, | |
npxG_p90, xA_p90, npxG, xA) | |
glimpse(goal_contribution_clean_df) | |
``` | |
## plot | |
### goals & assists | |
```{r fig.width = 11, fig.height = 9} | |
## Description text | |
desc_raul <- "El Lobo Alfa: With 8 goals (7.6 xG) & 6 assists (4.5 xA) Raúl Jiménez has been the fulcrum of Wolves' push for the Champions League." | |
desc_aubaings <- "S.O.S., Help Needed: Scoring 50% (HALF!) of their team's goals, Ings (0.55 xG per 90, 5th best in the league) & Auba (0.52 xG per 90, 10th) keep their team's hopes alive!" | |
desc_buendia <- "One of the few bright lights in the Canaries' season, Buendía has impressed with 6 assists & 0.32 xA per 90 (4th best in the league)!" | |
## PLOT! | |
goal_contribution_matrix <- goal_contribution_clean_df %>% | |
ggplot(aes(assist_contrib, goal_contrib)) + | |
geom_point(data = goal_contribution_clean_df %>% | |
filter(goal_contrib < 0.2 | assist_contrib < 0.2), | |
color = "grey20", size = 4, alpha = 0.2) + | |
geom_point(data = goal_contribution_clean_df %>% | |
filter(goal_contrib > 0.2 | assist_contrib > 0.15), | |
color = "red", size = 4) + | |
geom_hline(yintercept = 0.2, color = "grey20", alpha = 0.4) + | |
geom_vline(xintercept = 0.15, color = "grey20", alpha = 0.4) + | |
geom_text_repel( | |
data = goal_contribution_clean_df %>% | |
filter(goal_contrib > 0.2 | assist_contrib > 0.15, | |
!player %in% c("R. Jiménez", "P. Aubameyang", | |
"D. Ings", "E. Buendía")), | |
aes(label = player, family = "Roboto Condensed", fontface = "bold"), | |
seed = 15, size = 4, | |
min.segment.length = 0, segment.color = "red", | |
point.padding = 0.5) + | |
geom_mark_circle( | |
aes(filter = player == "R. Jiménez", | |
label = "Raúl Jiménez", | |
description = desc_raul), | |
label.width = unit(90, 'mm'), label.buffer = unit(5, "mm"), | |
label.family = "Roboto Condensed", label.fontsize = c(14, 12)) + | |
geom_mark_hull( | |
aes(filter = player %in% c("P. Aubameyang", "D. Ings"), | |
label = "Auba & Ings", | |
description = desc_aubaings), | |
label.buffer = unit(5, "mm"), label.fontsize = c(14, 11), | |
label.family = "Roboto Condensed") + | |
geom_mark_hull( | |
aes(filter = player == "E. Buendía", | |
label = "Emi Buendía", | |
description = desc_buendia), | |
concavity = 1, | |
label.width = unit(65, 'mm'), label.buffer = unit(0.5, "mm"), | |
label.fontsize = c(14, 12), label.family = "Roboto Condensed") + | |
scale_x_continuous(labels = percent_format(accuracy = 1), | |
breaks = c(0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3), | |
limits = c(0, 0.35)) + | |
scale_y_continuous(labels = percent_format(accuracy = 1), | |
breaks = c(0, 0.1, 0.2, 0.3, 0.4, 0.5), | |
limits = c(0, 0.57)) + | |
labs(title = "Goal Involvement: <b style='color: #38003c'>Premier League</b> (2019-2020 Season)", | |
subtitle = glue(" | |
Goal Involvement (Goals or Assists) as Percentage of Total Club Goals | |
Matchday 20 | January 1st, 2020"), | |
caption = glue(" | |
Data: FBref | StatsBomb | |
Ryo Nakagawara, Twitter: @R_by_Ryo"), | |
x = "Percentage of Club Goals Assisted", | |
y = "Percentage of Club Goals Scored") + | |
theme_minimal() + | |
theme(text = element_markdown(family = "Roboto Condensed"), | |
plot.title = element_markdown(size = 20), | |
plot.subtitle = element_text(size = 16), | |
plot.caption = element_text(size = 14), | |
axis.title = element_text(size = 14), | |
axis.text = element_text(size = 12), | |
panel.grid.minor.x = element_blank()) | |
goal_contribution_matrix | |
``` | |
#### save | |
```{r} | |
ggsave(plot = goal_contribution_matrix, | |
here::here("Premier League 2019-2020/output/goal_contribution_plot_1920_MD20.png"), | |
height = 9, width = 11) | |
``` | |
```{r} | |
plot_logo <- add_logo( | |
plot_path = here::here("Premier League 2019-2020/output/goal_contribution_plot_1920_MD20.png"), | |
logo_path = "https://upload.wikimedia.org/wikipedia/en/f/f2/Premier_League_Logo.svg", | |
logo_position = "top right", | |
logo_scale = 6) | |
plot_logo | |
``` | |
```{r} | |
image_write(image = plot_logo, | |
here::here("Premier League 2019-2020/output/goal_contribution_plot_1920_MD20_logo.png")) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment