First, you should load recharts
:
library(recharts)
Tree chart includes 4 basic types:
The keys are:
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.y
facet
is used to produce separate treesechartr(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 |
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 |
facet |
facetting variable which will be coerced to factors. Each level of |
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 |
|
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.
type
is set ‘tree’.
echartr(data[data$series=='Tree 1',], c(node, parent), value, type='tree') %>%
setTitle('Single Tree')
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')
Set type
‘tree_inv’. Here you can test subtype
s.
echartr(data, c(node, parent), value, facet=series, type='tree_inv',
subtype='broken') %>% setTitle('tree_inv')
Set type
‘htree’.
echartr(data, c(node, parent), value, facet=series, type='htree',
subtype='broken + dashed') %>% setTitle('htree')
Set type
‘htree_inv’.
echartr(data, c(node, parent), value, facet=series, type='htree_inv',
subtype='broken + dotted') %>% setTitle('htree_inv')
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.