Interactive Collapsible Tree Diagrams using 'D3.js'

Interactive Reingold-Tilford tree diagrams created using 'D3.js', where every node can be expanded and collapsed by clicking on it. Tooltips and color gradients can be mapped to nodes using a numeric column in the source data frame. See 'collapsibleTree' website for more information and examples.

CRAN_Status_Badge CRAN downloads

collapsibleTree is an R htmlwidget that allows you to create interactive collapsible Reingold-Tilford tree diagrams using D3.js, adapted from Mike Bostock's example. Turn your data frame into a hierarchical visualization without worrying about nested lists or JSON objects!

If you're using Shiny, you can bind the most recently clicked node to a Shiny input, allowing for easier interaction with complex nested objects. The input will return a named list containing the most recently selected node, as well as all of its parents. See the Shiny example for more info.


# Alternately, install the latest development version from GitHub:
# install.packages("devtools")

Changelog can be found here.


When working with data in R, it makes sense (at least to me) to represent everything as a data frame. I'm a big fan of tidy data, but this structure does not lend itself to easily designing hierarchical networks.

collapsibleTree uses data.tree to handle all of that, freeing you from a lot of recursive list construction.

Click here to see some interactive charts.

collapsibleTree(warpbreaks, c("wool", "tension", "breaks"))

Collapsible Tree

The color of each node can be customized to draw attention to the levels of hierarchy. Thanks to Ryan Derickson for the implementation idea! Colors can be constants or generated from a gradient function.

# Data from US Forest Service DataMart
species <- read.csv("")
  hierarchy = c("REGION", "CLASS", "NAME"), 
  fill = c(
    # The root
    # Unique regions
    rep("brown", length(unique(species$REGION))),
    # Unique classes per region
    rep("khaki", length(unique(paste(species$REGION, species$CLASS)))),
    # Unique names per region
    rep("forestgreen", length(unique(paste(species$NAME, species$REGION))))

Collapsible Tree Colored

Gradients can be mapped to a column in the data frame to help visualize relative weightings of nodes. Node weighting can also be mapped to a tooltip.

  c("wool", "tension", "breaks"),
  attribute = "breaks",
  maxPercent = 50

Collapsible Tree Gradient

Likewise, node size can also be mapped to a column in the data frame to help visualize relative weightings of nodes.

  c("wool", "tension", "breaks"),
  attribute = "breaks",
  maxPercent = 50,
  nodeSize = "breaks",
  collapsed = FALSE

Collapsible Tree Gradient

Parent-child relationships can be mapped to the tree to give more customizability for each node, such as passing custom html elements to each node.

# Create a simple org chart
org <- data.frame(
  Manager = c(
    NA, "Ana", "Ana", "Bill", "Bill", "Bill", "Claudette", "Claudette", "Danny",
    "Fred", "Fred", "Grace", "Larry", "Larry", "Nicholas", "Nicholas"
  Employee = c(
    "Ana", "Bill", "Larry", "Claudette", "Danny", "Erika", "Fred", "Grace",
    "Henri", "Ida", "Joaquin", "Kate", "Mindy", "Nicholas", "Odette", "Peter"
  Title = c(
    "President", "VP Operations", "VP Finance", "Director", "Director", "Scientist",
    "Manager", "Manager", "Jr Scientist", "Operator", "Operator", "Associate",
     "Analyst", "Director", "Accountant", "Accountant"
# Add in colors and sizes
org$Color <- org$Title
levels(org$Color) <- colorspace::rainbow_hcl(11)
# Use unsplash api to add in random photos to tooltip
org$tooltip <- paste0(
  "<br>Title: ",
  "<br><img src=''>"
  attribute = "Title",
  fill = "Color",
  nodeSize = "leafCount",
  tooltipHtml = "tooltip"

Collapsible Tree Network

Shiny Interaction

An interactive Shiny demo is also included. For example, you could use the collapsibleTree htmlwidget to select a portion of a larger categorical dataset, with your filter being as deep or shallow as you'd prefer. You can find a live demo here, or run the included examples locally.

# Basic Shiny Interaction
shiny::runApp(system.file("examples/02shiny", package = "collapsibleTree"))
# Interactive Gradient Mapping
shiny::runApp(system.file("examples/03shiny", package = "collapsibleTree"))

Issues and Suggestions

Feel free to submit an issue if you run into any bugs or have any feature suggestions! Would love to hear your comments.

Test Results

#> [1] "Sat Sep 23 12:40:42 2017"
#> Error handling: .........
#> Margin sizing: ................
#> Missing values: ....
#> Network: ......
#> Root labelling: ..........
#> DONE ======================================================================


Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.1.6 by Adeel Khan, 5 months ago,

Report a bug at

Browse source code at

Authors: Adeel Khan [aut, cre], Dhrumin Shah [ctb], Mike Bostock [ctb, cph] (D3.js library,

Documentation:   PDF Manual  

GPL (>= 3) license

Imports htmlwidgets, data.tree, stats, methods

Suggests colorspace, RColorBrewer, dplyr, testthat

Enhances knitr, shiny

See at CRAN