2 min read

Week29 - d3treeR 2

Week29 - d3treeR 2

htmlwidgets News This Week

A gallery is up thanks to Ryan Hafen, but not “official”. As always, for all the newest and updated htmlwidgets, just do this Github search.

This Week’s Widget - d3treeR Again

d3treeR was released last week in the post Week 28 | d3treeR. We’ll do d3treeR again but we’ll make a version 2 d3tree2 using the versions from Mike Bostock’s Zoomable Treemaps and Zan Armstrong’s fork Zoomable Treemaps with Color.

This d3tree2 version should work much better when there are multiple levels of nesting or if you want a small in size d3tree. All it takes is one, so thanks so much for @ignacio82 for the motivation.

Quick Installation

d3treeR is not yet on CRAN and won’t be unless someone asks, so for now please install with devtools::install_github. If you installed last week, you’ll need to do this again.

devtools::install_github("timelyportfolio/d3treeR")

Examples

Let’s look at the differences between d3tree and d3tree2 in the examples below. We’ll use the same examples as last week.

treemap as data

As stated in the introduction, d3treeR is built to work seamlessly with treemaps from the R treemap package.

# devtools::install_github("timelyportfolio/d3treeR")
library(treemap)
library(d3treeR)

# first example from ?treemap

data(GNI2010)

tm <-  treemap(
   GNI2010,
   index=c("continent", "iso3"),
   vSize="population",
   vColor="GNI",
   type="value"
 )


d3tree( tm,rootname = "World" )

d3tree2( tm,rootname = "World" )

# devtools::install_github("timelyportfolio/d3treeR")
library(treemap)
library(d3treeR)

# last example from ?treemap

data(business)
business$employees.growth <- business$employees - business$employees.prev

tm <- # Brewer's Red-White-Grey palette reversed with predefined range
  treemap(business,
          index=c("NACE1", "NACE2"),
          vSize="employees",
          vColor="employees.growth",
          type="value",
          palette="-RdGy",
          range=c(-30000,30000))

d3tree( tm, rootname = "World" )

d3tree2( tm, rootname = "World" )

JSON as data

We can use JSON or lists from R to provide our hierarchy. Let’s use the ubiquitous flare.json.

library(treemap)
library(d3treeR)

  # with flare.json from Mike Bostock

d3tree(
  "http://bl.ocks.org/mbostock/raw/4063582/raw/flare.json"
  ,id = "name"
  ,height = 520
  ,width = 620
)

d3tree2(
  "http://bl.ocks.org/mbostock/raw/4063582/raw/flare.json"
  ,id = "name"
  ,height = 520
  ,width = 620
)

Thanks

Thanks so much to Martijn Tennekes, Edwin de Jonge, Mike Bostock, Bill White, and Zan Armstrong.

As always, thanks to

  • Ramnath Vaidyanathan and RStudio for htmlwidgets
  • all the contributors to R and JavaScript