Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
EPL graph
# devtools::install_github('jalapic/engsoccerdata')
library(engsoccerdata)
library(dplyr)
library(plotly)
library(htmlwidgets)
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())
#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)
#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))
)
p <- ggplot(df, aes(x = gameno, y = cumpts)) +
geom_path(aes(color = Season, key = Season), alpha = 0.5) +
facet_wrap(~ team) + ggtitle("Each Team's EPL Record") +
xlab("Game in Season") + ylab("Cumulative Points") +
newggtheme
p %>%
ggplotly(tooltip = "colour") %>%
as.widget() %>%
onRender('
function(el, x) {
var graphDiv = document.getElementById(el.id);
// reduce the opacity of every trace except for the hover one
el.on("plotly_hover", function(e) {
var traces = [];
var curve = e.points[0].curveNumber;
var pt = e.points[0].pointNumber;
var key = x.data[curve].key[pt];
for (var i = 0; i < x.data.length; i++) {
if (x.data[i].key[0] !== key) traces.push(i);
}
Plotly.restyle(graphDiv, "opacity", 0.05, traces);
})
el.on("plotly_unhover", function(e) {
var traces = [];
for (var i = 0; i < x.data.length; i++) traces.push(i);
Plotly.restyle(graphDiv, "opacity", 1, traces);
})
}
')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment