Skip to content

Instantly share code, notes, and snippets.

View JoGall's full-sized avatar

Joe Gallagher JoGall

View GitHub Profile
@JoGall
JoGall / hoodability_set_pieces.Rmd
Last active August 7, 2017 13:56
On 'Hoodability' and conceding set piece goals
Data on set piece goals scored and conceded in 2016-17 from [here](https://objective-football.blogspot.co.uk/2016/08/201617-open-playset-piecepenalty-data.html).
---
# Get data
```{r}
library(dplyr)
library(XML)
library(RCurl)
library(engsoccerdata)
@JoGall
JoGall / FPL_price_changes.R
Last active September 9, 2017 09:59
Compute net changes in player value for each gameweek in FPL (2016-17, 2017-18)
require(dplyr)
require(ggplot2)
# get week-by-week data for current season and last (data originally taken from official API)
d17 <- read.csv(url("https://github.com/JoGall/FantasyPL/blob/master/17-18.csv"))
d16 <- read.csv(url("https://github.com/JoGall/FantasyPL/blob/master/16-17_formatted.csv"))
# compute price changes
ss16 <- d16 %>%
group_by(player_id) %>%
@JoGall
JoGall / xy_speed_pos.R
Last active October 1, 2017 17:45
Extract speed and direction from x,y-coordinates
# dataframe 'dat' w/ x,y-coords in columns 'x' and 'y' and timestamp in column, 'ts', e.g.:
library(soccermatics)
attach(tromso)
dat <- subset(tromso, id == 8)[,c(1,3:4)]
#calculate direction of each vector angle (in radians)
v <- diff(complex(real = dat$x, imaginary = dat$y))
dat$direction <- c(NA, diff(Arg(v)) %% (2*pi), NA) - pi
# calculate speed (distance / time interval)
@JoGall
JoGall / gglexis.R
Last active November 13, 2017 00:39
Lexis lifeline ggplot
library(ggplot2)
library(dplyr)
library(scales)
# SAMPLE DATA
set.seed(123)
# get all dates in a range
start_date <- as.Date("2017-01-01")
end_date <- as.Date("2017-06-01")
dates <- seq(start_date, end_date, 1)
@JoGall
JoGall / drawNetballCourt.R
Last active January 29, 2018 13:29
Draw a netball court using ggplot
library(ggplot2)
library(ggforce) # gives us `geom_circle` and `geom_arc` functions
# pitch dimensions
courtLength <- 30.5
courtWidth <- 15.25
courtRunoff <- 3.05
# aesthetic parameters
pitch_col <- "grey90"
# Computes number of neighbours in common between first degree neighbourhood of each vertex in an igraph network
# Not sure if this is meaningful metric yet but seems like it could be interesting!
# FUNCTION --------------------------------------------------------------------
common.neighbours <- function(graph) {
lapply(unique(V(graph)), function(v) {
# get vertices of 1st-order neighbourhood for focal vertex (and omit self)
library(tidyverse)
library(soccermatics)
library(StatsBombR)
# get data from StatsBomb
fixture <- FreeMatches(37) %>%
filter(home_team.home_team_name == "Reading WFC" & away_team.away_team_name == "Yeovil Town LFC")
# helper function from my fork of statsbombr package: https://github.com/JoGall/StatsBombR
# or code here: https://github.com/JoGall/StatsBombR/blob/master/R/allinfo.R
#devtools::install_github("statsbomb/StatsBombR") #install package
#devtools::install_github("jogall/StatsBombR") #or fork for R v<5.0 if necessary
library(StatsBombR)
# see all free competitions
FreeCompetitions()
# e.g. get all fixtures from FIFA World Cup
fixtures <- FreeMatches(43)
@JoGall
JoGall / possessionAdjust.R
Last active March 13, 2019 09:57
Possession adjust metrics for StatsBomb data
# Compute total minutes played per player -------------------------------
statsbombMinsPlayed <- function(dat) {
dat <- dat %>%
group_by(match_id) %>%
mutate(end = max(ElapsedTime))
# starting XI
minutes <- dat %>%
player_id age elo prop_mins_played
1 18 60 0.330277778
1 19 60 0.620325203
1 20 60 0.791725768
1 21 60 0.791472868
1 22 60 0.868888889
1 23 70 0.631860776
1 24 70 0.850793651
1 25 70 0.5
1 25 80 0.627664399