I have some hierarchical data, e.g.,
> df <- data_frame(id = 1:6, parent_id = c(NA, 1, 1, 2, 2, 5))
Source: local data frame [6 x 2]
1 1 NA
2 2 1
3 3 1
4 4 2
5 5 2
6 6 5
Since the aggregated data should have a similar structure to a treemap, we can use the
treemap package to do the aggregation (could also use a loop with successive aggregation). Then,
d3treeR (from github) is used to convert the treemap data to a nested list, and
jsonlite to convert the list to json.
I'm using some example data
GNI2010, found in the
d3treeR package. You can see all of the source files on plunker.
library(treemap) library(d3treeR) # devtools::install_github("timelyportfolio/d3treeR") library(data.tree) library(jsonlite) ## Get treemap data using package treemap ## Using example data GNI2010 from d3treeR package ## aggregate by these: continent, iso3, ## size by population, and color by GNI indexList <- c('continent', 'iso3') treedat <- treemap(GNI2010, index=indexList, vSize='population', vColor='GNI', type="value", fun.aggregate = "sum", palette = 'RdYlBu') treedat <- treedat$tm # pull out the data ## Use d3treeR to convert to nested list structure ## Call the root node 'flare' so we can just plug it into the example res <- d3treeR:::convert_treemap(treedat, rootname="flare") ## Convert to JSON using jsonlite::toJSON json <- toJSON(res, auto_unbox = TRUE) ## Save the json to a directory with the example index.html writeLines(json, "d3circle/flare.json")
I also replaced the source line in the example
To create the shiny bindings should be doable using
htmlwidgets and following some examples (the
d3treeR source has some). Note that certain things aren't working, like the coloring. The json that gets stored here actually contains a lot of information about the nodes (all the data aggregated using the
treemap) that you could leverage in the figure.