Learn R Programming

⚠️There's a newer version (2.5.6) of this package.Take me there.

Treemapify provides ggplot2 geoms for drawing treemaps

Install

Install ggplot2, reshape2, plyr, devtools and ggfittext if you don't have them already. ggfittext and treemapify are installed from github.

install.packages("ggplot2")
install.packages("reshape2")
install.packages("plyr")
install.packages("devtools")
library(devtools)
install_github("wilkox/ggfittext")
install_github("wilkox/treemapify")

Example

For this example, we'll plot some data on the G-20 group of major world economies. treemapify includes this in the G20 data frame:

library(treemapify)
G20
RegionCountryGDP.mil.USDHDIEcon.classification
AfricaSouth Africa3843150.629Developing
North AmericaUnited States156847500.937Advanced
North AmericaCanada18190810.911Advanced
North AmericaMexico11771160.775Developing
South AmericaBrazil23959680.730Developing
South AmericaArgentina4749540.811Developing
AsiaChina82270370.699Developing
AsiaJapan59639690.912Advanced
AsiaSouth Korea11558720.909Advanced
AsiaIndia18248320.554Developing
AsiaIndonesia8781980.629Developing
EurasiaRussia20219600.788Developing
EurasiaTurkey7944680.722Developing
EuropeEuropean Union164144830.876Advanced
EuropeGermany34005790.920Advanced
EuropeFrance26086990.893Advanced
EuropeUnited Kingdom24405050.875Advanced
EuropeItaly20140790.881Advanced
Middle EastSaudi Arabia7273070.782Developing
OceaniaAustralia15417970.938Advanced

A treemap has a tile for each observation, with the area of the tile proportional to a variable. Let's start by drawing a treemap with each tile representing a G-20 country. The area of the tile will be mapped to the country's GDP, and the tile's fill colour mapped to its HDI (Human Development Index). geom_treemap is the basic geom for this purpose.

ggplot(G20, aes(area = GDP.mil.USD, fill = HDI)) +
  geom_treemap()

This plot isn't very useful without the knowing what country is represented by each tile. geom_treemap_text can be used to add a text label to each tile. It uses the ggfittext package to resize the text so it fits the tile. In addition to standard text formatting aesthetics you would use in geom_text, like ‘fontface’ or ‘colour’, we can pass additional options specific for ggfittext. For example, we can place the text in the centre of the tile with place = "centre", and expand it to fill as much of the tile as possible with grow = TRUE.

ggplot(G20, aes(area = GDP.mil.USD, fill = HDI, label = Country)) +
  geom_treemap() +
  geom_treemap_text(
    fontface = "italic",
    colour = "white",
    place = "centre",
    grow = TRUE
  )

geom_treemap supports subgrouping of tiles within a treemap by passing a ‘subgroup’ aesthetic. Let's subgroup the countries by region, draw a border around each subgroup with geom_treemap_subgroup_border, and label each subgroup with geom_treemap_subgroup_text. As with geom_treemap_text, geom_treemap_subgroup_text can take ggfittext parameters for text placement and sizing.

ggplot(G20, aes(
  area = GDP.mil.USD,
  fill = HDI,
  label = Country,
  subgroup = Region,
  )) +
  geom_treemap() +
  geom_treemap_subgroup_border() +
  geom_treemap_subgroup_text(
    place = "centre",
    grow = T,
    alpha = 0.5,
    colour = "black",
    fontface = "italic",
    min.size = 0
  ) +
  geom_treemap_text(
    colour = "white",
    place = "topleft",
    reflow = T
  )

Note that ‘Argentina’ has been hidden. geom_treemap_text will hide text labels that cannot fit a tile without being shrunk below a minimum size, by default 4 points. This can be adjusted with the ‘min.size’ option.

Like any ggplot2 plot, treemapify plots can be faceted, scaled, themed, etc.

ggplot(G20, aes(area = GDP.mil.USD, fill = Region, label = Country)) +
  geom_treemap() +
  geom_treemap_text(grow = T, reflow = T, colour = "black") +
  facet_wrap( ~ Econ.classification) +
  scale_fill_brewer(palette = "Set1") +
  theme(legend.position = "bottom") +
  labs(
    title = "The G-20 major economies",
    caption = "The area of each country is proportional to its relative GDP
               within the economic group (advanced or developing)",
    fill = "Region"
  )

Animated treemaps

All the treemapify treemaps support a ‘fixed’ option that will cause the tiles to be laid out in an order determined by the order of observations in the data frame. While this can result in a less aesthetically pleasing and readable treemap, it allows you to create animated treemaps with the help of the tweenr and gganimate packages.

library(tweenr)
library(gganimate)

G20_alt <- G20
G20_alt$GDP.mil.USD <- sample(G20$GDP.mil.USD, nrow(G20))
G20_alt$HDI <- sample(G20$HDI, nrow(G20))

tweened <- tween_states(list(G20, G20_alt, G20), tweenlength = 15, statelength = 5, ease = 'cubic-in-out', nframes = 55)

animated_plot <- ggplot(tweened, aes(
  area = GDP.mil.USD,
  fill = HDI,
  label = Country,
  subgroup = Region,
  frame = .frame
  )) +
  geom_treemap(fixed = T) +
  geom_treemap_subgroup_border(fixed = T) +
  geom_treemap_subgroup_text(
    place = "centre",
    grow = T,
    alpha = 0.5,
    colour = "black",
    fontface = "italic",
    min.size = 0,
    fixed = T
  ) +
  geom_treemap_text(
    colour = "white",
    place = "topleft",
    reflow = T,
    fixed = T
  )

animation::ani.options(interval = 1/15)
gganimate(animated_plot, "animated_treemap.gif", title_frame = F, ani.width = 400, ani.height = 400)

Credit

treemapify uses the Squarified Treemap algorithm of Mark Bruls, Kees Huizing and Jarke van Wijk.

The G20 dataset is from the Wikipedia article G-20 major economies, which is released under the Creative Commons Attribution-Share-Alike License 3.0.

Copy Link

Version

Install

install.packages('treemapify')

Monthly Downloads

7,573

Version

2.3.1

License

GPL (>=3)

Issues

Pull Requests

Stars

Forks

Maintainer

David Wilkins

Last Published

September 30th, 2023

Functions in treemapify (2.3.1)

geom_treemap

A treemap
geom_treemap_subgroup_border

Draw a border around a subgroup of treemap tiles.
treemapify

Generate coordinates for a treemap with a squarified layout
treemapify_fixed

Generate coordinates for a treemap with a fixed layout
geom_treemap_subgroup_text

Text labels for treemap subgroups.
geom_treemap_text

Add text labels to treemap tiles.
GeomTreemap

GeomTreemap
ggplotify

Draw an exploratory treemap