This is part of a series of visualisations for the forthcoming Ashes between Australia and England

The Ashes 2017/18: Career Runs

A quick way to interactively visualise the cumulative career runs of each potential squad member from Australia and England over their career. To do this without shiny, you can use Plotly + Crosstalk.

Data used in this is scrapped from espncricinfo using the rvest package.

library(plotly)
library(crosstalk)
library(tidyverse)

library(rvest)

as a subplot

# scrapped from espncricinfo
aus_batting <- getAusBatting()
eng_batting <- getEngBatting()

# clean data for this vis
df <- subset(aus_batting, select = c(Runs, Player))
df$Runs <- as.numeric(df$Runs)
df[is.na(df)] = 0

df2 <- subset(eng_batting, select = c(Runs, Player))
df2$Runs <- as.numeric(df2$Runs)
df2[is.na(df2)] = 0

# innings and cumsum of runs
df <- df %>% 
  group_by(Player) %>% 
  mutate(id = row_number(), cs = cumsum(Runs))

df2 <- df2 %>% 
  group_by(Player) %>% 
  mutate(id = row_number(), cs = cumsum(Runs))

# hoverinfo
info <- ~paste("Player: ", Player,
               "<br>Innings: ", id,
               "<br>Career Runs:", cs)

#shared data
sd <- SharedData$new(df, ~Player, group = "Choose a Player")
sd2 <- SharedData$new(df2, ~Player, group = "Choose a Player")

p <- sd %>%
  plot_ly(
    x = ~id, y = ~cs, type = 'scatter', mode = 'lines', alpha = 0.4, showlegend = F,
    text = info, hoverinfo = 'text', color = I('blue')) %>%
  layout(
    xaxis = list(
      showgrid = F,
      zeroline = F,
      range = c(0, 450)), 
    yaxis = list(
      title = "Career Runs",
      showgrid = F,
      zeroline = F))


pp <- sd2 %>%
  plot_ly(
    x = ~id, y = ~cs, type = 'scatter', mode = 'lines', alpha = 0.4, showlegend = F,
    text = info, hoverinfo = 'text', color = I('red')) %>%
  layout(
    xaxis = list(
      showgrid = F,
      zeroline = F,
      range = c(0, 450)), 
    yaxis = list(
      title = "Career Runs",
      showgrid = F,
      zeroline = F))

s <- subplot(p,pp, shareY = T) %>% 
  highlight(selectize = TRUE, on = 'plotly_click', off = 'plotly_doubleclick',
            persistent = TRUE)

as a single plot

It may be easier to compare and contrast as a single plot:

aus_batting2 <- df
aus_batting2$Team <- rep("Aus", nrow(aus_batting2))
eng_batting2 <- df2
eng_batting2$Team <- rep("Eng", nrow(eng_batting2))

career_batting <- rbind(aus_batting2,eng_batting2)

sd3 <- SharedData$new(career_batting, ~Player, group = "Choose a Player")

info <- ~paste("Player: ", Player,
               "<br>Innings: ", id,
               "<br>Career Runs:", cs)

cb <- sd3 %>%
  plot_ly(
    x = ~id, y = ~cs, type = 'scatter', mode = 'lines', alpha = 0.4, showlegend = F,
    text = info, hoverinfo = 'text', color = ~Team, colors = c('blue','red')) %>%
  layout(
    xaxis = list(
      title = "Innings",
      showgrid = F,
      zeroline = F),
    yaxis = list(
      title = "Career Runs",
      showgrid = F,
      zeroline = F)
    ) %>% 
  highlight(
    selectize = TRUE, on = 'plotly_click', 
    off = 'plotly_doubleclick', persistent = TRUE
  )