block by timelyportfolio a772301f6f477f83e65398552a6f90d9

zoomable treemap bar charts

Full Screen

Built with blockbuilder.org and R htmlwidgets

Chris Given @cmgiven has been posting a d3.js block/day for the last 12 days and has committed to a total of 15. I like to honor such fine work, and the only way I know how to honor his achievement is to make an htmlwidget out of my favorite of his masterpieces. I decided to use d3kit as an extra bonus, so that the chart will be reusable for both the JavaScript and R communities.

I had originally said I would stick to his 2 hour maximum rule, but I got carried away. I still have a lot of ideas for improvement.

R package

Github R package treebar

Live example

live example

Code to replicate

#devtools::install_github("timelyportfolio/treebar")

library(stringr)
library(treebar)
library(jsonlite)

## make it a more generic hierarchy
##  normally this step is not necessary
json <- str_replace_all(
  readLines(system.file("example/data.json",package="treebar")),
  "(country)|(continent)|(year)|(type)",
  "id"
)

data <- fromJSON(json, simplifyDataFrame=FALSE)

treebar(data)


# also allows different treemap tiling options
library(htmltools)

browsable(
  tagList(
    lapply(
      c("Squarify", "Binary", "SliceDice", "Slice", "Dice"),
      function(tile){
        tags$div(
          style = "float:left; display:inline;",
          tags$h3(tile),
          treebar(
            data,
            tile = tile, 
            height = 250,
            width = 400
          )
        )
      }
    )   
  )
)


# use different key for id and value
json <- str_replace_all(
  readLines("./inst/example/data.json"),
  "(country)|(continent)|(year)|(type)",
  "name"
)

json <- str_replace_all(
  json,
  "(value)",
  "size"
)

data <- fromJSON(json, simplifyDataFrame=FALSE)

treebar(data, value="size", id="name")