library(rtemis.draw)14 Treemaps and sunburst charts
draw_treemap() and draw_sunburst() nest a table’s rows by the columns named in levels, outermost first. A row’s path ends at its first missing level, the rows that end at one node sum their value, and without value each row counts as 1. A node that both ends a row and has children is an error, since its children give its value. Siblings follow the factor level order of their column, otherwise their order of first appearance:
places <- data.frame(
region = c("Europe", "Europe", "Europe", "Asia", "Asia", "Asia", "Americas", "Americas"),
country = c("France", "Spain", "Italy", "Japan", "India", "China", "USA", "Brazil"),
population = c(68, 48, 59, 124, 1428, 1410, 335, 216)
)
draw_treemap(places, levels = c("region", "country"), value = "population")A sunburst draws one ring per level. label_depth sets how many rings, from the center out, carry labels, and label_rotate turns them:
draw_sunburst(places, levels = c("region", "country"), value = "population")14.1 Configuration
setup_TreemapConfig() and setup_SunburstConfig() describe the two charts by their level and value columns:
config <- setup_SunburstConfig(levels = c("region", "country"),
value = "population", label_depth = 1L)
draw(config, data = places)