# json list hierarchical data representation
jsonl <- jsonlite::fromJSON('[
{"name": "earth", "value": 30,
"children": [
{"name": "land", "value":10,
"children": [
{"name": "forest", "value": 3},
{"name": "river", "value": 7}
]},
{"name": "ocean", "value":20,
"children": [
{"name": "fish", "value": 10,
"children": [
{"name": "shark", "value":2},
{"name": "tuna", "value":6}
]},
{"name": "kelp", "value": 5}
]}
]
},
{"name": "mars", "value": 30,
"children": [
{"name": "crater", "value": 20},
{"name": "valley", "value": 20}
]},
{"name": "venus", "value": 40, "itemStyle": {"color": "blue"} }
]', simplifyDataFrame = FALSE)
jsonl |>
e_charts() |>
e_sunburst() # demo
# tibble hierarchical data representation
library(dplyr)
df <- tibble(
name = c("earth", "mars", "venus"),
value = c(30, 40, 30),
# 1st level
itemStyle = tibble(color = c(NA, "red", "blue")),
# embedded styles, optional
children = list(
tibble(
name = c("land", "ocean"),
value = c(10, 20),
# 2nd level
children = list(
tibble(name = c("forest", "river"), value = c(3, 7)),
# 3rd level
tibble(
name = c("fish", "kelp"),
value = c(10, 5),
children = list(
tibble(name = c("shark", "tuna"), value = c(2, 6)),
# 4th level
NULL # kelp
)
)
)
),
tibble(name = c("crater", "valley"), value = c(20, 20)),
NULL # venus
)
)
df |>
e_charts() |>
e_sunburst() |>
e_theme("westeros")
# with styles
myStyles <- c(list(color = "green"), list(color = "magenta")) # custom styles defined
myNames <- list(c("land", "river"), "crater") # names to style
myLevels <- list(2, c(3, 4)) # hierarchical levels to style
df |>
e_charts() |>
e_sunburst(myStyles, myNames, myLevels)
Run the code above in your browser using DataLab