Treemap with subgroups



This post explains how to build a treemap with subgroups in R. It uses the treemap package, provides reproducible code and explains how input data must be formatted.

Treemap section Data to Viz

This is a treemap with several levels. We have 3 groups, each containing several subgroups. Each subgroup has a value which we map to an area.

In the index argument, you need to specify levels in the order of importance: group > subgroup > sub-subgroup.

Note: If you have one level of grouping only, see chart #234

Note: Showing more than 2 levels often result in a cluttered and unredable figure. Why not considering an interactive version?

# library
library(treemap)
 
# Build Dataset
group <- c(rep("group-1",4),rep("group-2",2),rep("group-3",3))
subgroup <- paste("subgroup" , c(1,2,3,4,1,2,1,2,3), sep="-")
value <- c(13,5,22,12,11,7,3,1,23)
data <- data.frame(group,subgroup,value)
 
# treemap
treemap(data,
            index=c("group","subgroup"),
            vSize="value",
            type="index"
            ) 

Related chart types


Grouped and Stacked barplot
Treemap
Doughnut
Pie chart
Dendrogram
Circular packing



❤️ 10 best R tricks ❤️

👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥