Skip to content

Instantly share code, notes, and snippets.

@OTStats
Created November 7, 2022 02:48
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 OTStats/5dcaf8e71d88a5bdbe3b4b86992720e1 to your computer and use it in GitHub Desktop.
Save OTStats/5dcaf8e71d88a5bdbe3b4b86992720e1 to your computer and use it in GitHub Desktop.
# -- Load libraries
# library(cowplot)
# library(gt)
library(tidyverse)
library(showtext)
library(worldfootballR)
library(ggtext)
library(janitor)
library(glue)
library(ggsoccer)
library(ggimage)
library(lubridate)
# library(ggpattern)
# `%!in%` <- Negate(`%in%`)
source("R/add_logo.R")
## Loading Google fonts (http://www.google.com/fonts)
font_add_google("Lato")
showtext_auto()
# league_matches <- fotmob_get_league_matches(
# country = "ESP",
# league_name = "LaLiga"
# )
doParallel::registerDoParallel()
dates <- seq(ymd("20220220"), Sys.Date()-1, by = "day")
results <- fotmob_get_matches_by_date(date = dates)
filtered_results <- results %>%
dplyr::select(primary_id, ccode, league_name = name, matches) %>%
dplyr::filter(league_name == "Major League Soccer", ccode == "USA")
# one way of getting data out of the results
unnested_results <- filtered_results %>%
tidyr::unnest_longer(col = matches) %>%
unnest(everything()) %>%
unnest(cols = c("home", "away"), names_sep = "_") %>%
unnest(cols = status) %>%
janitor::clean_names()
match_ids <- unnested_results %>%
filter(home_name == "Nashville SC" | away_name == "Nashville SC") %>%
distinct() %>%
pull(id)
details_df <- fotmob_get_match_details(match_ids)
shots <- details_df %>%
unnest(shots) %>%
mutate(team = if_else(team_id == home_team_id, home_team, away_team)) %>%
# team_id = if_else(team_id == home_team_id, home_team_color, away_team_color)) %>%
filter(team == "Nashville SC",
player_name == "Hany Mukhtar") %>%
unnest(on_goal_shot, names_sep = "_")
map <- shots %>%
filter(is_on_target, !is_blocked) %>%
mutate(color_adjusted = if_else(event_type == "Goal", "#ffd60a", team_color)) %>%
ggplot(aes(on_goal_shot_x, on_goal_shot_y)) +
#Draw posts
geom_segment(aes(x=0,xend=0,y=0,yend=0.66534392)) +
geom_segment(aes(x=0.028571429,xend=0.028571429,y=0,yend=0.66534392 - 0.028571429)) +
geom_segment(aes(x=2,xend=2,y=0,yend=0.66534392)) +
geom_segment(aes(x=2-0.028571429,xend=2-0.028571429,y=0,yend=0.66534392 - 0.028571429)) +
geom_segment(aes(x=0,xend=2,y=0.66534392,yend=0.66534392)) +
geom_segment(aes(x=0.028571429,xend=2-0.028571429,y=0.66534392 - 0.028571429,yend=0.66534392 - 0.028571429)) +
# -- Draw Net --
# # Vertical lines
geom_segment(aes(x=0.1,xend=.1, y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=0.2,xend=.2, y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=0.3,xend=.3, y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=0.4,xend=.4, y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=0.5,xend=.5, y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=0.6,xend=.6, y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=0.7,xend=.7, y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=0.8,xend=.8, y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=0.9,xend=.9, y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=1, xend=1, y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=1.1,xend=1.1,y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=1.2,xend=1.2,y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=1.3,xend=1.3,y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=1.4,xend=1.4,y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=1.5,xend=1.5,y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=1.6,xend=1.6,y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=1.7,xend=1.7,y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=1.8,xend=1.8,y=0,yend=0.66534392- 0.028571429), alpha =.007) +
geom_segment(aes(x=1.9,xend=1.9,y=0,yend=0.66534392- 0.028571429), alpha =.007) +
# # Horizontal lines
geom_segment(aes(x=0.028571429,xend=2-0.028571429,y=.055,yend=.055), alpha =.007) +
geom_segment(aes(x=0.028571429,xend=2-0.028571429,y=.11,yend=.11), alpha =.007) +
geom_segment(aes(x=0.028571429,xend=2-0.028571429,y=.165,yend=.165), alpha =.007) +
geom_segment(aes(x=0.028571429,xend=2-0.028571429,y=.22,yend=.22), alpha =.007) +
geom_segment(aes(x=0.028571429,xend=2-0.028571429,y=.275,yend=.275), alpha =.007) +
geom_segment(aes(x=0.028571429,xend=2-0.028571429,y=.33,yend=.33), alpha =.007) +
geom_segment(aes(x=0.028571429,xend=2-0.028571429,y=.385,yend=.385), alpha =.007) +
geom_segment(aes(x=0.028571429,xend=2-0.028571429,y=.44,yend=.44), alpha =.007) +
geom_segment(aes(x=0.028571429,xend=2-0.028571429,y=.495,yend=.495), alpha =.007) +
geom_segment(aes(x=0.028571429,xend=2-0.028571429,y=.55,yend=.55), alpha =.007) +
geom_segment(aes(x=0.028571429,xend=2-0.028571429,y=.605,yend=.605), alpha =.007) +
geom_segment(aes(x=-.2,xend=2.2,y=0,yend=0), alpha =1) +
geom_point(aes(size = expected_goals, color = color_adjusted), alpha = 0.9) +
scale_y_continuous(expand = c(0, .01), limits = c(0, .75)) +
scale_color_identity() +
coord_fixed() +
# theme_void() +
labs(title = "Hany Mukhtar | Nashville SC",
subtitle = "<span style='color:#1E1742;font-weight:bold'>Shots on target</span> and <span style='color:#ffd60a;font-weight:bold'>goals</span> in MLS between Feb 27 - Aug 14 2022",
caption = "Created by: Owen Thompson | @OTStats",
size = "xG") +
theme(text = element_text(family = "Lato"),
plot.background = element_rect(color = "#ffffff", linetype = NULL),
panel.background = element_rect(color = "#ffffff", linetype = NULL),
plot.title = element_text(size = 24),
plot.subtitle = element_markdown(size = 14),
legend.position = "bottom") +
theme(panel.grid = element_blank(),
panel.background = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.key = element_rect(fill = NA)) +
labs(x = NULL,
y = "")
epl_player_xg_2021 <- fotmob_get_season_stats(
country = "USA",
league_name = "MLS",
season = "2022",
stat_name = "Expected goals (xG)",
team_or_player = "player"
)
# test_df <- worldfootballR::fb_player_season_stats(player_url = "https://fbref.com/en/players/87fe8526/Hany-Mukhtar",
# stat_type = "shooting")
pacman::p_load(kableExtra)
table <- shots %>%
summarize(Matches = n_distinct(match_id) + 1,
`Total Shots` = n(),
`Shots on Target` = sum(is_on_target),
Goals = sum(event_type == "Goal"),
xG = round(sum(expected_goals), digits = 2),
xGOT = round(sum(expected_goals_on_target, na.rm = TRUE), digits = 2),
) %>%
kable() %>%
kable_styling()
as_image(file = "plots/table.png", width = 2.25)
table_image <- magick::image_read("plots/table.png")
map %>%
patchwork::inset_element(table, left = .3, bottom = 0, top = .2, right = .6)
ggsave(filename = "plots/20220811-Hany Mukhtar MLS SOT.png",
plot = last_plot(),
width = 7.5, height = 5, dpi = "retina")
goal_image <- magick::image_read("plots/20220811-Hany Mukhtar MLS SOT.png")
@OTStats
Copy link
Author

OTStats commented Nov 7, 2022

20220815-Hany Mukhtar SOT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment