This is part of a series of
A quick way to interactively visualise the cumulative career wickets 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)
# scrapped from espncricinfo
aus_bowling <- getAusBowling()
eng_bowling <- getEngBowling()
# clean data for this vis
df <- subset(aus_bowling, select = c(Wkts, Player))
df$Wkts <- as.numeric(df$Wkts)
df[is.na(df)] = 0
df2 <- subset(eng_bowling, select = c(Wkts, Player))
df2$Wkts <- as.numeric(df2$Wkts)
df2[is.na(df2)] = 0
# innings and cumsum of runs
df <- df %>%
group_by(Player) %>%
mutate(id = row_number(), cs = cumsum(Wkts))
df2 <- df2 %>%
group_by(Player) %>%
mutate(id = row_number(), cs = cumsum(Wkts))
# hoverinfo
info <- ~paste("Player: ", Player,
"<br>Innings: ", id,
"<br>Career Wickets:", 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 Wickets",
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 Wickets",
showgrid = F,
zeroline = F))
s <- subplot(p,pp, shareY = T) %>%
highlight(selectize = TRUE, on = 'plotly_click', off = 'plotly_doubleclick',
persistent = TRUE)
It may be easier to compare and contrast as a single plot:
aus_bowling2 <- df
aus_bowling2$Team <- rep("Aus", nrow(aus_bowling2))
eng_bowling2 <- df2
eng_bowling2$Team <- rep("Eng", nrow(eng_bowling2))
career_bowling <- rbind(aus_bowling2,eng_bowling2)
sd3 <- SharedData$new(career_bowling, ~Player, group = "Choose a Player")
info <- ~paste("Player: ", Player,
"<br>Innings: ", id,
"<br>Career Wickets:", 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 Wickets",
showgrid = F,
zeroline = F)
) %>%
highlight(
selectize = TRUE, on = 'plotly_click',
off = 'plotly_doubleclick', persistent = TRUE
)