Let’s explore the package ‘collapsableTree’ using NFL data. I foraged the internet to create a fairly simple csv file with columns Sport, Division, Region, Team, and Cap_Spend. The values in Cap_Spend are the team’s active (2020) active salary spends per OverTheCap.com.
Let’s get R set-up and then we can explore.
# Set CRAN Mirror
options(repos = c(CRAN = "http://cran.rstudio.com"))
# Set time zone
options(tz="America/New_York")
We’re going to use the collapsableTree library to create the visualization. Documentation is available here and here.
# Load data set
library(readxl)
Football_Caps <- read_excel("~/Desktop/Football_Caps.xls")
head(Football_Caps)
## # A tibble: 6 x 5
## Sport Division Region Team Cap_Spend
## <chr> <chr> <chr> <chr> <dbl>
## 1 NFL AFC East Patriots 152419613
## 2 NFL AFC East Bills 137125758
## 3 NFL AFC East Dolphins 101477629
## 4 NFL AFC East Jets 149532263
## 5 NFL AFC North Ravens 140712424
## 6 NFL AFC North Browns 170049829
# Load needed libraries
library(collapsibleTree)
The basic tree is fairly straight-forward to create.
collapsibleTree(Football_Caps,
hierarchy = c("Division","Region","Team"),
width = 500)
Let’s also show the salary cap information that we added. We’ll need the dplyr package to do this. To get the cumulative spending for each branch of the hierarchy, we’ll use a sum function. Make sure you hover over each node and notice that the exact salary spending is listed.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
Football_Caps %>%
group_by(Division, Region, Team) %>%
summarize(`Salary Spending` = sum(Cap_Spend)) %>%
collapsibleTreeSummary(
hierarchy = c("Division","Region","Team"),
root = "Football_Caps",
width = 500,
attribute = "Salary Spending")
What if we just wanted variation in the colors. I like blue, so we’ll have each node be a shade of blue.
divisionColors <- RColorBrewer::brewer.pal(length(unique(Football_Caps$Division)), "Blues")
## Warning in RColorBrewer::brewer.pal(length(unique(Football_Caps$Division)), : minimal value for n is 3, returning requested palette with 3 different levels
# Regions will be a gradient that resets between divisions
regionColors <- Football_Caps %>%
arrange(Division, Region) %>%
group_by(Division) %>%
distinct(Region) %>%
mutate(colors = colorspace::sequential_hcl(length(Region))[seq_along(Region)])
# Teams will also be a gradient that resets between divisions, but not Regions
teamColors <- Football_Caps %>%
arrange(Division, Region) %>%
group_by(Division) %>%
distinct(Team) %>%
mutate(colors = colorspace::sequential_hcl(length(Team))[seq_along(Team)])
Football_Caps %>%
arrange(Division, Region, Team) %>%
collapsibleTree(
hierarchy = c("Division", "Region", "Team"),
root = "Football_Caps",
width = 500,
fill = c(divisionColors, regionColors$colors, teamColors$colors))
That’s all for now…
[THE END]