Skip to content

Instantly share code, notes, and snippets.

@padpadpadpad
Last active December 27, 2017 10:36
Show Gist options
  • Save padpadpadpad/fbf237e0bbe3e92237a9496aa711aba2 to your computer and use it in GitHub Desktop.
Save padpadpadpad/fbf237e0bbe3e92237a9496aa711aba2 to your computer and use it in GitHub Desktop.
Use engsoccerdata to plot league tables through time
# Get league tables from engsoccerdata
# Daniel Padfield
# load in packages
library(engsoccerdata)
library(dplyr)
library(tidyr)
library(ggplot2)
# load in data
data("england")
names(england)
# create a league table for all season
d_tables <- group_by(england, Season, tier) %>%
do(., data.frame(maketable_all(df = .))) %>%
ungroup()
# filter for 1995 onwards
d_1995_onwards <- filter(d_tables, Season >=1995) %>%
mutate(., Pos = as.numeric(Pos))
ggplot(d_1995_onwards, aes(Pos, Pts, group = Season, col = Season)) +
geom_line(alpha = 0.5) +
facet_wrap(~ tier, scales = 'free_x') +
scale_x_reverse()
# get current premier league table
d_2017_PL <- england_current() %>%
filter(., tier == 1) %>%
do(data.frame(maketable_all(df = .))) %>%
mutate(., Pos = as.numeric(Pos))
# plot
ggplot(d_2017_PL, aes(forcats::fct_reorder(team, Pos, .desc = TRUE), Pts)) +
geom_line(aes(group = 1)) +
geom_point() +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
xlab('') +
ylab('Points') +
geom_vline(aes(xintercept = 3.5), linetype = 2, col = 'red') +
geom_vline(aes(xintercept = 16.5), linetype = 2) +
ggtitle('Premier league table 2017/2018 after 19 games')
# save
ggsave('~/Desktop/Premier_league_table.png', height = 5, width = 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment