|
| 1 | +library(engsoccerdata) |
| 2 | +library(dplyr) |
| 3 | +library(tidyr) |
| 4 | +library(plotly) |
| 5 | +library(crosstalk) |
| 6 | + |
| 7 | +# shape data into desired format |
| 8 | +dat <- england %>% |
| 9 | + gather(location, team, home, visitor) %>% |
| 10 | + # focus on tier 1 teams that are still playing in 2015 |
| 11 | + filter(team %in% maketable_eng(england, 2015, 1)[["team"]]) %>% |
| 12 | + mutate( |
| 13 | + pts = ifelse(location == "home" & goaldif > 0, 3, |
| 14 | + ifelse(location == "away" & goaldif < 0, 3, 1)) |
| 15 | + ) %>% |
| 16 | + arrange(Date) %>% |
| 17 | + group_by(Season, team) %>% |
| 18 | + mutate(gameno = row_number(), cumpts = cumsum(pts)) %>% |
| 19 | + ungroup() %>% |
| 20 | + group_by(gameno) %>% |
| 21 | + mutate(meanP = mean(cumpts)) %>% |
| 22 | + filter(Season > 2006) |
| 23 | + |
| 24 | +sd <- SharedData$new(dat, ~team, "Select a team") |
| 25 | + |
| 26 | +# a 'wormchart' like fig 8 here http://www.gradaanwr.net/wp-content/uploads/2016/06/dataApr16.pdf |
| 27 | +p <- ggplot(sd, aes(x = gameno, y = cumpts - meanP)) + |
| 28 | + geom_line(aes(group = team), alpha = 0.5) + |
| 29 | + facet_wrap(~ Season, ncol = 3) + |
| 30 | + labs( |
| 31 | + title = "English Premier League Performance", |
| 32 | + x = "Game in season", |
| 33 | + y = "Cumulative points (above/below) average" |
| 34 | + ) |
| 35 | + |
| 36 | +gg <- ggplotly(p, width = 1050, height = 600, tooltip = "team") |
| 37 | + |
| 38 | +highlight( |
| 39 | + gg, |
| 40 | + persistent = TRUE, |
| 41 | + dynamic = TRUE, |
| 42 | + selectize = TRUE, |
| 43 | + color = RColorBrewer::brewer.pal(12, "Paired") |
| 44 | +) |
0 commit comments