treebar: Interactive Zoomable Treemap Bar Charts

Description Usage Arguments Examples

Description

htmlwidget based off Chris Given's Treemap Bar

Usage

1
treebar(data = NULL, ..., width = NULL, height = NULL, elementId = NULL)

Arguments

data

string json data of a four level d3.js hierarchy. inflexible now but will improve

...

additional arguments currently supports id, name, and tile for customizing your chart

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#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")
library(treebar)
library(data.tree)

portfolio <- data.frame(
  year = rep(2014:2016, each=8),
  asset = c(rep("equity",5),rep("fixed",3)),
  subasset = c("infotech","infotech","energy","energy","telecom","invgrade","invgrade","highyield"),
  ticker = rep(c("msft","apple","xom","cvx","t","pttrx","vficx","vwehx"),3),
  value = runif(24,50000,250000),
  stringsAsFactors = FALSE
)

portfolio$pathString <- paste(
  "portfolio", 
  portfolio$year, 
  portfolio$asset,
  portfolio$subasset,
  portfolio$ticker,
  sep = "/"
)

portfolio_tree <- as.Node(portfolio)

treebar(
  ToListExplicit(portfolio_tree, unname=TRUE),
  id = "name"
)

# also allows different 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(
            ToListExplicit(portfolio_tree, unname=TRUE),
            id = "name",
            tile = tile, 
            height = 250,
            width = 320
          )
        )
      }
    )   
  )
)


# play with treemap treepalette
library(treemap)
library(treebar)
library(dplyr)

portfolio %>%
  mutate(year = as.character(year)) %>%
  inner_join(treepalette(.,index=c("asset","subasset","ticker"))) %>%
  mutate(color = HCL.color) %>%
  select(-starts_with("HCL")) %>%
  d3r::d3_nest(value_cols=c("value","color")) %>%
  treebar()
#devtools::install_github("timelyportfolio/treebar")

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

## 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)

shinyApp(
  ui = htmlwidgets::onRender(
    treebar(data),
    htmlwidgets::JS(
'
function(el, x){
  var chart = HTMLWidgets.getInstance(el).instance.treebar;
  chart.on("nodeMouseover", function(d,i){
    Shiny.onInputChange("treebar_mouseover", d.data);
  });
}
'    
    )
  ),
  server = function(input, output, session){
    observeEvent(input$treebar_mouseover,{
      print(input$treebar_mouseover)
    })
  }
)

timelyportfolio/treebar documentation built on May 31, 2019, 2:15 p.m.