File: crosstalk-highlight-epl.R

package info (click to toggle)
r-cran-plotly 4.10.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 30,636 kB
  • sloc: javascript: 195,272; sh: 24; makefile: 6
file content (29 lines) | stat: -rw-r--r-- 930 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
library(engsoccerdata)
library(dplyr)
library(tidyr)
library(plotly)

# shape data into desired format
dat <- england %>% 
  gather(location, team, home, visitor) %>% 
  # focus on tier 1 teams that are still playing in 2015
  filter(team %in% maketable_eng(england, 2015, 1)[["team"]]) %>%
  mutate(
    pts = ifelse(location == "home" & goaldif > 0, 3, 
                 ifelse(location == "away" & goaldif < 0, 3, 1))
  ) %>%
  arrange(Date) %>%
  group_by(Season, team) %>% 
  mutate(gameno = row_number(), cumpts = cumsum(pts))

sd <- highlight_key(dat, ~Season, "Select a season")

p <- ggplot(sd, aes(x = gameno, y = cumpts)) + 
  geom_line(aes(color = Season, group = Season), alpha = 0.5) + 
  facet_wrap(~ team) + ggtitle("English Premier League Performance (1888-2015)") +
  xlab("Game in Season") + ylab("Cumulative Points") 

gg <- ggplotly(p, tooltip = "colour")

highlight(gg, opacityDim = 0.05, selectize = TRUE)