block by timelyportfolio dcc552df6983f0b03236715cbb21a30b

d3 treemap of football game time

Full Screen

assembled with blockbuilder.org and R htmlwidgets

I started with a sunburst and flamegraph in my head (see block), but a treemap with d3treeR was so easy that I could not resist.

library(htmltools)
library(xml2)
library(rvest)
library(pipeR)
library(dplyr)
library(lubridate)
# devtools::install_github("timelyportfolio/d3treeR")
library(d3treeR)
library(d3r)

# Alabama vs Texas A&M
url <- "http://rolltide.com/boxscore.aspx?id=4471&path=football"

# make simple converter for minutes:seconds to seconds
#  based off http://stackoverflow.com/questions/29067375/r-convert-hoursminutesseconds
minsec_to_sec <- function(tim){
  res <- lubridate::ms(tim)       # format to 'hours:minutes:seconds'
  lubridate::minute(res)*60 + lubridate::second(res)
}

url %>>%
  read_html() %>%
  html_node("section#all-drives table") %>>%
  html_table(fill=TRUE, header=FALSE) %>>%
  {
    cnm <- .[2,]
    tbl <- .[-(1:2),]
    colnames(tbl) <- cnm
    tbl
  } %>>%
  {.[,c(2,3,9,11)]} %>>%
  mutate(
    Qtr = gsub(x=Qtr.,pattern="[a-z,A-Z]",replacement=""),
    result = `How Lost`,
    size = minsec_to_sec(TOP)
  ) %>>%
  select(Team, Qtr, result, size) %>>%
  ({df<-.}) %>>%
  d3r::d3_nest(value_cols="size") %>>%
  d3treeR::d3tree2(value="size")

# to do with treemap + d3treeR
treemap(
  df,
  index = c("Team", "Qtr", "result"),
  vSize = "size",
  vColor = "result",
  type = "categorical"
) %>>%
  d3treeR::d3tree2()