First, you should load recharts:

library(recharts)

1 Introduction

Tree chart includes 4 basic types:

  • Horizontal tree
  • Inverse horizontal tree
  • Vertical tree
  • Inverse vertical tree

The keys are:

  • character x with at least 2 columns: x[,1] are the nodes of the trees, x[,2] are parent nodes of the tree nodes. For root nodes, parent should be NA.
  • numeric y
  • facet is used to produce separate trees

2 Function Call

echartr(data, x, <y>, <series>, <facet>, <t>, <type>, <subtype>)
Arg Requirement

data

source data in the form of data.frame

x

character independent variable. Each level of x is treated as a data series. Other type will be coerced to factors. Only the first two are accepted if multiple variables are provided: 1st one as nodes, 2nd one as parent nodes.

y

numeric dependent variable. Only the first one is accepted if multiple variables are provided.

series

series variable which will be coerced to factors. Each level of series is treated as a grouping factor. Only the first one is accepted if multiple variables are provided.

facet

facetting variable which will be coerced to factors. Each level of facet is treated as a subsetting factor to produce separate trees. Only the first one is accepted if multiple variables are provided.

t

timeline variable which will be coerced to factors. Only the first one is accepted if multiple variables are provided.

type

‘tree’/‘vtree’, ‘tree_inv’/‘vtree_inv’, ‘htree’, ‘htree_inv’.

subtype

  • tree: c(“curve”,“broken”,“dotted”,“dashed”,“solid”)
    • curve: nodes are connected by curve
    • broken: nodes are connected by broken lines
    • dotted: the connection lines are dotted
    • solid: the connection lines are solid
    • dashed: the connection lines are dashed
  • tree_inv: c(“curve”,“broken”,“dotted”,“dashed”,“solid”)
  • htree: c(“curve”,“broken”,“dotted”,“dashed”,“solid”)
  • htree_inv: c(“curve”,“broken”,“dotted”,“dashed”,“solid”)

3 Showcase

3.1 Data Preparation

Let’s prepare a data.frame to show the typical structure.

data <- data.frame(
    node=c('North America', 'USA', 'Canada', 'California', 'New York', 
           'Massachusetts', 'Quebec', 'Ontario', 'South America',
           'Brazil', 'Argentina', 'Rio de Jeneiro', 'Sao Paolo', 'Buenos Aires',
           'Mendosa'),
    parent=c(NA, rep('North America', 2), rep('USA', 3), rep('Canada', 2),
             NA, rep('South America', 2), rep('Brazil', 2), rep('Argentina', 2)),
    series=c(rep('Tree 1', 8), rep('Tree 2', 7)),
    value=rep(1, 15))
knitr::kable(data)
node parent series value
North America NA Tree 1 1
USA North America Tree 1 1
Canada North America Tree 1 1
California USA Tree 1 1
New York USA Tree 1 1
Massachusetts USA Tree 1 1
Quebec Canada Tree 1 1
Ontario Canada Tree 1 1
South America NA Tree 2 1
Brazil South America Tree 2 1
Argentina South America Tree 2 1
Rio de Jeneiro Brazil Tree 2 1
Sao Paolo Brazil Tree 2 1
Buenos Aires Argentina Tree 2 1
Mendosa Argentina Tree 2 1

The tree structure is defined by the mapping of nodes and parents.

  • There are two series ‘Tree 1’ and ‘Tree 2’, so we will see two trees.
    • In the series ‘Tree 1’, we see ‘North America’ has NA parent node. So ‘North America’ will be the root node of this tree.
      • Below the root, we see 2 levels in parents nodes: ‘USA’ and ‘Canada’. So ‘Tree 1’ tree will contain 2 level-1 nodes under the root ‘North America’.
        • There are 3 nodes with ‘USA’ as parent: ‘California’, “New York” and “Massachusetts”
        • There are 2 nodes with ‘Canada’ as parent: ‘Ontario’ and ‘Quebec’

3.2 Tree Chart

3.2.1 Single Tree

type is set ‘tree’.

echartr(data[data$series=='Tree 1',], c(node, parent), value, type='tree') %>%
    setTitle('Single Tree')

3.2.2 Multiple Trees

If you assign series with a valid data structure, you yield multiple trees. But Echarts does not map series with legend and color well. So you need to color the trees separately.

echartr(data, c(node, parent), value, facet=series, type='tree') %>%
    setTitle('Multiple Trees')

3.3 Inverse Tree Chart

Set type ‘tree_inv’. Here you can test subtypes.

echartr(data, c(node, parent), value, facet=series, type='tree_inv',
        subtype='broken') %>% setTitle('tree_inv')

3.4 Horizontal Tree

Set type ‘htree’.

echartr(data, c(node, parent), value, facet=series, type='htree', 
        subtype='broken + dashed') %>% setTitle('htree')

3.5 Inverse Horzontal Tree

Set type ‘htree_inv’.

echartr(data, c(node, parent), value, facet=series, type='htree_inv', 
        subtype='broken + dotted') %>% setTitle('htree_inv')

4 Futher Setup

Then you can configure the widgets, add markLines and/or markPoints, fortify the chart.

You can refer to related functions to play around on your own.