A quick bit of scraping and simple manipulation to pull in records-over-time for Atlantic Division teams, given we’re pretty much halfway through the season. All data courtesy of Hockey Reference.

First, identify the right URLs to find the historical data:

base_url <- "https://www.hockey-reference.com/teams/"
urls <- paste(base_url,
  c("TOR", "TBL", "BOS", "MTL", "FLA", "DET", "BUF"),
  "/2018_games.html", sep = "")

A basic scraping loop using lapply and rvest:

df_raw <- bind_rows(lapply(urls, function(x) {
  data.frame(
    url = x,
    wins = read_html(x) %>% html_nodes(css = '.center+ td') %>% html_text(),
    losses = read_html(x) %>% html_nodes(css = '.right:nth-child(11)') %>% html_text(),
    stringsAsFactors = FALSE)
}))

Convert to numeric, calculate games above/below .500, identify teams, and number each game:

df <- df_raw %>%
  mutate(wins = as.numeric(wins),
         losses = as.numeric(losses),
         record = wins - losses) %>%
  mutate(team = ifelse(grepl("TOR", url), "Toronto",
               ifelse(grepl("TBL", url), "Tampa Bay",
               ifelse(grepl("BOS", url), "Boston",
               ifelse(grepl("MTL", url), "Montreal",
               ifelse(grepl("FLA", url), "Florida",
               ifelse(grepl("DET", url), "Detroit",
               ifelse(grepl("BUF", url), "Buffalo", NA)))))))) %>%
  group_by(team) %>%
  mutate(game_no = row_number())

Visualize with highcharter:

df %>%
  na.omit() %>%
  hchart("line", hcaes(x = game_no, y = record, group = team)) %>%
  hc_tooltip(table = TRUE, sort = TRUE) %>%
  hc_xAxis(title = list(text = "Game Number (1 to most recent)")) %>%
  hc_yAxis(title = list(text = "Number of games above (or below) .500")) %>%
  hc_colors(c("#CDD8D9", "#CDD8D9", "#CDD8D9", "#CDD8D9", "#CDD8D9", "#CDD8D9", "#003E7E")) %>%
  hc_add_theme(hc_theme_google())

It isn’t that hard to get the same data for all 30 teams and aggregate up to look at goals for vs. goals against. Tampa Bay and Boston are clearly the elite teams this season offensively and defensively. Arizona and Buffalo are struggling at both ends:

Goals per shot ratio vs. wins — teams that score on a higher percentage of their shots tend to win more:

And penalty minutes — Nashville leads the league in taking penalties, yet still manages to win. Make of that what you will: