echarty

Minimal R/Shiny Interface to ECharts.js


CRAN   twitter

Use Case 3 - Hierarchies in R and echarty

There are several types of charts to represent hierarchical structures - sunburst, tree, treemap, sankey, graph. Hierarchical data in R can be built with:

Many users find the latter representation most intuitive and straightforward. However note that it requires library data.tree.
Note also the difference in data setting for chart tree compared to sunburst and treemap. Tree’s data is the top-level node, while sunburst and treemap use the first lower level. Here are examples for the three types.

List of lists

data <- list(list(name='Grandpa',children=list(
  list(name='Uncle Leo',value=15,
     children=list(list(name='Cousin Jack',value=2), 
                   list(name='Cousin Mary',value=5,
     children=list(list(name='Jackson',value=2))), 
                   list(name='Cousin Ben',value=4))),
  list(name='Father',value=10,children=list(list(name='Me',value=5),
      list(name='Brother Peter',value=1))))), 
  list(name='Nancy',children=list(list(name='Uncle Nike',children=list(list(name='Cousin Betty',value=1), list(name='Cousin Jenny',value=2))))))
# -------------------------------------------------

p <- ec.init(preset=FALSE)
p$x$opts$series <- list(list(type = 'sunburst', 
  data = data, radius=list(0, '90%'), label=list(rotate='radial')))
p

Data frame with nested children

animl <- data.frame(name = "Animals", value = 1)  # top level
animl$children <- 
	list(data.frame(name = c("Mammals", "Reptiles","Fish"), value = c(100,90,60)))
animl$children[[1]]$children <- c(
 	list(data.frame(name = c("Dogs", "Humans"), value = c( 15, 35))),
 	list(data.frame(name = c("Snakes", "Lizards"), value = c(30, 40))),
 	list(data.frame(name = c("Sharks"), value = 30)) )
animl$children[[1]]$children[[3]]$children <- 
	list(data.frame(name = c("hammerhead", "thresher"), value = c(10,20)))
# -------------------------------------------------

p <- ec.init(preset=FALSE)
p$x$opts$series <- list(list(type='tree', 
    data = jsonlite::toJSON(animl), 
    label = list(offset=c(0, -12)), 
    symbolSize = htmlwidgets::JS("function(d) { return d; }") ))  # size by value
p

p$x$opts$series <- list(list(type='treemap', 
    data=jsonlite::toJSON(animl), leafDepth=1)) 
p

p$x$opts$series <- list(list(type='sunburst', 
    data=jsonlite::toJSON(animl$children[[1]]), 
    radius=c(0, '90%'), label=list(rotate='radial') ))
p


Data frame with parent-child

df <- data.frame(parents = c("","Reptiles", "Reptiles", "Mammals", "Mammals", "Fish", "Sharks", "Sharks", "Animals", "Animals", "Animals"),
                 children = c("Animals", "Snakes", "Lizards", "Dogs", "Humans", "Sharks", "hammerhead", "thresher", "Reptiles", "Mammals", "Fish"),
                 value = c(55, 30, 40, 15, 35, 30, 10, 20, 90, 100, 60)) 
library(data.tree)
tmp <- data.tree::FromDataFrameNetwork(df)
json <- data.tree::ToListExplicit(tmp, unname=TRUE)
# -------------------------------------------------

p <- ec.init(preset=FALSE)
p$x$opts$series <- list(list(type='sunburst', 
    data=json$children[[1]]$children, 
    radius=c(0, '90%'), label=list(rotate='radial'), emphasis=list(focus='ancestor') ))
p

p$x$opts$series <- list(list(type='tree', 
    data=json$children, 
    label = list(offset = c(0, -12)), symbolSize = htmlwidgets::JS("function(d) { return d; }") ))
p

p$x$opts$series <- list(list(type='treemap', 
    data=json$children[[1]]$children, leafDepth=1))
p$x$opts$tooltip <- list(ey='')   # ey is a dummy parameter, in JS becomes object "tooltip:{ey:''}"
p
sunburst