Last active
November 18, 2021 15:22
-
-
Save jmcastagnetto/16bae7aac1cb3fc5e95d8ae77d52ce03 to your computer and use it in GitHub Desktop.
Example of use of echarts4r to make a treemap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# based on the example at: https://echarts4r.john-coene.com/articles/chart_types.html#treemap | |
# the JS example at: https://echarts.apache.org/examples/en/editor.html?c=treemap-show-parent | |
# and the documentation at: https://echarts.apache.org/en/option.html#series-treemap.type | |
library(tidyverse) | |
library(echarts4r) | |
# this data structure does not work with the changed function | |
# set.seed(13579) | |
# | |
# tm <- tribble( | |
# ~parent, ~child, | |
# "Earth", "Forest", | |
# "Earth", "Ocean", | |
# "Earth", "Iceberg", | |
# "Mars", "Elon", | |
# "Mars", "Curiosity", | |
# "Discworld", "The Rim", | |
# "Discworld", "Ankh-Morpork", | |
# "Discworld", "A'Tuin" | |
# ) %>% add_column( | |
# value = ceiling(rnorm(8, 10, 2)) | |
# ) | |
# the new version of the e_treemap() function in echarts4r 0.4.2 | |
# expects hierarchical data, here I am creating it stepwise so it | |
# is clear how it should be structured | |
earth <- tribble( | |
~name, ~value, | |
"Forest", 40, | |
"Ocean", 40, | |
"Iceberg", 20 | |
) | |
mars <- tribble( | |
~name, ~value, | |
"Elon", 20, | |
"Curiosity", 80 | |
) | |
discworld <- tribble( | |
~name, ~value, | |
"The Rim", 15, | |
"Ankh-Morpork", 12, | |
"A'Tuin", 11 | |
) | |
tm <- tribble( | |
~name, ~value, ~children, | |
"Earth", 26, earth, | |
"Mars", 20, mars, | |
"Discworld", 38, discworld | |
) | |
tm %>% | |
e_charts() %>% | |
e_treemap( | |
leafDepth = 1, #drilldown | |
#breadcrumb = FALSE, #remove it | |
backgroundColor = "red", | |
itemStyle = list( | |
normal = list( | |
borderWidth = 0, | |
gapWidth = 2, | |
backgroundColor = "gray70" | |
) | |
), | |
name = "Having fun with treemaps", | |
upperLabel = list( | |
normal = list( | |
show = TRUE, | |
height = 30, | |
formatter = "{b}", | |
color = "black", | |
fontSize = 24 | |
) | |
) | |
)%>% | |
e_tooltip() %>% | |
e_labels(show = TRUE, | |
verticalAlign = "top", | |
fontSize = 24, | |
formatter = "{b}\n{@value} Thaums" | |
) %>% | |
e_title("Treemap charts", | |
textStyle = list(fontSize = 36), | |
textVerticalAlign = "top", | |
left = "center", | |
backgroundColor = "#CD853F64") %>% | |
e_theme("dark") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jmcastagnetto This is very much appreciated. I will study your code and learn from it.