Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
EPL 2015/16 graphs
library(devtools)
install_github('jalapic/engsoccerdata', username = "jalapic")
library(engsoccerdata)
library(dplyr)
head(england)
england$Date <- as.Date(england$Date, format="%Y-%m-%d")
#Get Data into Format Needed
df<-rbind(
england %>%
filter(tier==1) %>%
select(Date, Season, team=home,opp=visitor,gf=hgoal,ga=vgoal) %>%
mutate(venue="h", pts = ifelse(gf>ga, 3, ifelse(ga>gf,0,1)))
,
england %>%
filter(tier==1) %>%
select(Date, Season, team=visitor,opp=home,gf=vgoal,ga=hgoal) %>%
mutate(venue="a", pts = ifelse(gf>ga, 3, ifelse(ga>gf,0,1)))
) %>%
arrange(Date) %>%
group_by(team,Season) %>%
mutate(cumpts = cumsum(pts), gameno=row_number())
head(df)
tail(df)
#table to get levels
mylevs <- maketable_eng(england,2015,1) %>% .$team
myteams <- england %>% filter(Season==2015 & tier==1) %>% .$home %>% unique
df <- df %>% filter(team %in% myteams)
df$team<-factor(df$team, levels = mylevs)
library(ggplot2)
#custom ggplot theme for these data
newggtheme <- theme(
plot.title = element_text(hjust = 0, vjust = 1, size = rel(2.3)),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_blank(),
panel.border = element_rect(fill = NA, colour = "black"),
plot.background = element_blank(),
text = element_text(color = "gray20", size = 10),
axis.text = element_text(size = rel(1)),
axis.text.x = element_text(color = "gray20", size = rel(1.2)),
axis.text.y = element_text(color = "gray20", size = rel(1.2)),
axis.title.x = element_text(size = rel(1.3), vjust = 0),
axis.title.y = element_text(size = rel(1.3), vjust = 1),
axis.ticks.y = element_blank(),
axis.ticks.x = element_blank(),
strip.text.x = element_text(size = rel(1.5)),
legend.position = "none",
legend.key=element_rect(fill=NA),
legend.title = element_blank(),
legend.text=element_text(size=rel(1.5)),
plot.subtitle = element_text(color = "gray20", size = rel(1.0), face="italic"),
plot.caption = element_text(color = "dodgerblue", size = rel(1.0))
)
# set up colors
mycolors <- c('#0053A0','#EF0107','#001c58', '#5cBFEB','#ED1A3B','#DA020E','#60223B','#D00027',
'#E03A3E','#034694','#274488','#000000','#FFC20E','#091453','#1b458F','#EB172B',
'#EB172B','#000000','#FFe600','#7A003C')
df2015<-df %>% filter(Season==2015)
df2015$mycolor <- mycolors[match(df2015$team, mylevs)]
p <- ggplot(df, aes(x=gameno, y=cumpts, group=Season)) + geom_path(color='gray86',lwd=.2) + facet_wrap(~team) + newggtheme
p <- p + geom_path(data=df2015, aes(x=gameno, y=cumpts, color=mycolor), lwd=1.5) +
scale_color_identity()
p <- p + xlab("Game in Season") + ylab("Cumulative Points")
p + ggtitle("Each Team's EPL 2015/16 Record vs All Seasons", subtitle="Assuming 3 points for a win across all seasons") +
labs(caption = "@jalapic")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment