Skip to content

Instantly share code, notes, and snippets.

@tonyelhabr
Created October 17, 2022 16:59
Show Gist options
  • Save tonyelhabr/adff3051bd6068eff124840da782eccf to your computer and use it in GitHub Desktop.
Save tonyelhabr/adff3051bd6068eff124840da782eccf to your computer and use it in GitHub Desktop.
Fotmob shots for top scorers in the 2022/23 EPL season
suppressPackageStartupMessages({
  library(worldfootballR)
  library(dplyr)
  library(lubridate)
})

## Need the very most recent version of the package for `load_fotmob_match_details()` to work.
packageVersion("worldfootballR")
#> [1] '0.6.1.1000'

Fotmob's parent_league_season field always reflects the current season (data provider issue), so you would need to manually define the season by dates. (One could grab the first and last games of the season from fbref to assist with this.)

shots <- load_fotmob_match_details(
  country = 'ENG',
  league_name = 'Premier League'
) |> 
  mutate(
    date = strptime(match_time_utc, '%a, %b %d, %Y, %H:%M UTC', tz = 'UTC') |> date(),
  ) |> 
  filter(date >= ymd('2022-08-05'))
#> → Data last updated 2022-10-15 14:58:13 UTC

slim_shots <- shots |> 
  select(
    date,
    match_id,
    min,
    min_added,
    x, y, is_blocked, is_on_target,
    player_name,
    home_team,
    away_team,
    xg = expected_goals,
    xgot = expected_goals_on_target
  )

Select just the players we care about.

filt_shots <- slim_shots |> 
  filter(
    player_name %in% c(
      'Erling Braut Haaland',
      'Harry Kane',
      'Aleksandar Mitrovic',
      'Gabriel Jesus',
      'James Maddison',
      'Heung-Min Son'
    )
  )
filt_shots

#> # A tibble: 178 × 13
#>    date       match_id   min min_a…¹     x     y is_bl…² is_on…³ playe…⁴ home_…⁵
#>    <date>        <int> <int>   <int> <dbl> <dbl> <lgl>   <lgl>   <chr>   <chr>  
#>  1 2022-08-05  3900932     4      NA  93    34.2 TRUE    TRUE    Gabrie… Crysta…
#>  2 2022-08-06  3900933     1      NA  93.5  46.8 FALSE   FALSE   Aleksa… Fulham 
#>  3 2022-08-06  3900933    32      NA 102.   41.4 FALSE   TRUE    Aleksa… Fulham 
#>  4 2022-08-06  3900933    72      NA  94    34   FALSE   TRUE    Aleksa… Fulham 
#>  5 2022-08-06  3900938    31      NA  95.3  45.2 FALSE   TRUE    Heung-… Totten…
#>  6 2022-08-06  3900938    45       0  93.7  30.3 FALSE   FALSE   Heung-… Totten…
#>  7 2022-08-06  3900938    45       2  99.2  26.3 FALSE   TRUE    Harry … Totten…
#>  8 2022-08-06  3900938    68      NA  97.1  32.0 TRUE    TRUE    Heung-… Totten…
#>  9 2022-08-06  3900938    83      NA  90.5  40.2 FALSE   TRUE    Heung-… Totten…
#> 10 2022-08-06  3900938    90       1  45.6  30.3 FALSE   TRUE    Harry … Totten…
#> # … with 168 more rows, 3 more variables: away_team <chr>, xg <dbl>,
#> #   xgot <dbl>, and abbreviated variable names ¹​min_added, ²​is_blocked,
#> #   ³​is_on_target, ⁴​player_name, ⁵​home_team
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment