Treemap
ggplot2: geom_treemap() · Package: treemapify · Variables: 1-3 categorical levels + 1 numerical
WHAT IS A TREEMAP?
A treemap uses nested rectangles to represent hierarchical data where the area of each rectangle is proportional to its value. It solves the "pie chart with too many slices" problem by efficiently showing composition across many categories and sub-categories. Treemaps are widely used in finance (portfolio allocation, sector weights), disk space visualization, and organizational budgets. They answer "what takes up the most space/budget/market share?" and can show multiple levels of hierarchy. In R, use the treemapify package with geom_treemap() built on top of ggplot2.
BEST FOR
- · Hierarchical composition
- · Many categories (where pie chart fails)
- · Relative sizes
- · Portfolio allocation
AVOID WHEN
- · Non-hierarchical data
- · Precise comparison of similar sizes
- · Too many nesting levels
- · Negative values
R + GGPLOT2 CODE EXAMPLE
library(treemapify) df <- data.frame(car = rownames(mtcars), mpg = mtcars$mpg, cyl = factor(mtcars$cyl)) ggplot(df, aes(area = mpg, fill = cyl, label = car)) + geom_treemap() + geom_treemap_text(colour = "white", place = "centre", size = 8)
Run this code now
Paste the code above into RChat and see the treemap rendered instantly in your browser.