Draw Treemaps in 'ggplot2'

Provides 'ggplot2' geoms for drawing treemaps.

Travis-CI BuildStatus CRAN_Status_Badge

‘treemapify’ provides ‘ggplot2’ geoms for drawing treemaps.

Install the release version of ‘treemapify’ from CRAN:


If you want the development version, install it from GitHub:


The G20 dataset

‘treemapify’ includes an example dataset containing statistics about the G-20 group of major world economies.

#>           region        country gdp_mil_usd   hdi econ_classification
#> 1         Africa   South Africa      384315 0.629          Developing
#> 2  North America  United States    15684750 0.937            Advanced
#> 3  North America         Canada     1819081 0.911            Advanced
#> 4  North America         Mexico     1177116 0.775          Developing
#> 5  South America         Brazil     2395968 0.730          Developing
#> 6  South America      Argentina      474954 0.811          Developing
#> 7           Asia          China     8227037 0.699          Developing
#> 8           Asia          Japan     5963969 0.912            Advanced
#> 9           Asia    South Korea     1155872 0.909            Advanced
#> 10          Asia          India     1824832 0.554          Developing
#> 11          Asia      Indonesia      878198 0.629          Developing
#> 12       Eurasia         Russia     2021960 0.788          Developing
#> 13       Eurasia         Turkey      794468 0.722          Developing
#> 14        Europe European Union    16414483 0.876            Advanced
#> 15        Europe        Germany     3400579 0.920            Advanced
#> 16        Europe         France     2608699 0.893            Advanced
#> 17        Europe United Kingdom     2440505 0.875            Advanced
#> 18        Europe          Italy     2014079 0.881            Advanced
#> 19   Middle East   Saudi Arabia      727307 0.782          Developing
#> 20       Oceania      Australia     1541797 0.938            Advanced
#>    hemisphere
#> 1    Southern
#> 2    Northern
#> 3    Northern
#> 4    Northern
#> 5    Southern
#> 6    Southern
#> 7    Northern
#> 8    Northern
#> 9    Northern
#> 10   Northern
#> 11   Southern
#> 12   Northern
#> 13   Northern
#> 14   Northern
#> 15   Northern
#> 16   Northern
#> 17   Northern
#> 18   Northern
#> 19   Northern
#> 20   Southern

Drawing a simple treemap

In a treemap, each tile represents a single 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)) +

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)

Subgrouping tiles

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. geom_treemap_subgroup_text takes the same arguments for text placement and resizing as geom_treemap_text.

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 is not labelled. 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 argument.

Up to three nested levels of subgrouping are supported with the subgroup2 and subgroup3 aesthetics. Borders and text labels for these subgroups can be drawn with geom_treemap_subgroup2_border, etc. Note that ‘ggplot2’ draws plot layers in the order that they are added. This means it is possible to accidentally hide one layer of subgroup borders with another. Usually, it’s best to add the border layers in order from deepest to shallowest, i.e. geom_treemap_subgroup3_border then geom_treemap_subgroup2_border then geom_treemap_subgroup_border.

ggplot(G20, aes(area = 1, label = country, subgroup = hemisphere,
                subgroup2 = region, subgroup3 = econ_classification)) +
  geom_treemap() +
  geom_treemap_subgroup3_border(colour = "blue", size = 1) +
  geom_treemap_subgroup2_border(colour = "white", size = 3) +
  geom_treemap_subgroup_border(colour = "red", size = 5) +
  geom_treemap_subgroup_text(place = "middle", colour = "red", alpha = 0.5, grow = T) +
  geom_treemap_subgroup2_text(colour = "white", alpha = 0.5, fontface = "italic") +
  geom_treemap_subgroup3_text(place = "top", colour = "blue", alpha = 0.5) +
  geom_treemap_text(colour = "white", place = "middle", reflow = T)

As demonstrated, there is no assurance that the resulting plot will look good.

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

ggplot(G20, aes(area = gdp_mil_usd, fill = region, label = country, subgroup = region)) +
  geom_treemap() +
  geom_treemap_text(grow = T, reflow = T, colour = "black") +
  facet_wrap( ~ hemisphere) +
  scale_fill_brewer(palette = "Set1") +
  theme(legend.position = "bottom") +
    title = "The G-20 major economies by hemisphere",
    caption = "The area of each tile represents the country's GDP as a
      proportion of all countries in that hemisphere",
    fill = "Region"

Animated treemaps

The default algorithm for laying out the tiles is the ‘squarified’ algorithm. This tries to minimise the tiles’ aspect ratios, making sure there are no long and flat or tall and skinny tiles. While ‘squarified’ treemaps are aesthetically pleasing, the downside is that the position of tiles within the plot area can change dramatically with even small changes to the dataset. This makes it difficult to compare treemaps side-by-side, or create animated treemaps.

By providing the layout = "fixed" option to ‘treemapify’ geoms, an alternative layout algorithm is used that will always position the tiles based on the order of observations in the data frame. It’s very important that the same value for layout is passed to all ‘treemapify’ geoms, otherwise different layers of the plot might not share the same layout.

With the help of layout = "fixed", and with the tweenr and gganimate packages, it becomes possible to create animated treemaps showing e.g. change over time.

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 = 8,
                        statelength = 5, ease = 'cubic-in-out', nframes = 31)
animated_plot <- ggplot(tweened, aes(area = gdp_mil_usd, fill = hdi,
                                     label = country, subgroup = region,
                                     frame = .frame)) +
  geom_treemap(layout = "fixed") +
  geom_treemap_subgroup_border(layout = "fixed") +
  geom_treemap_subgroup_text(place = "centre", grow = T, alpha = 0.5,
                             colour = "black", fontface = "italic", min.size = 0,
                             layout = "fixed") +
  geom_treemap_text(colour = "white", place = "topleft", reflow = T, layout = "fixed")
animation::ani.options(interval = 1/10)
gganimate(animated_plot, "man/figures/animated_treemap.gif", title_frame = F,
          ani.width = 200, ani.height = 200)




Bug fixes

  • Fix issue with correct drawing of aesthetics in geom_tremap_subgroup*_text() (#32, thanks @jonathan-g)


Bug fixes

  • Fix issue with importing ggfittext


Major changes

  • Added new layout and start arguments to treemapify() and all geoms

Minor changes

  • Restore xlim and ylim options to treemapify()
  • Add and improve tests
  • Misc. code quality improvements

Bug fixes

  • Fixed bug in handling of subgroups, which should result in more aesthetically pleasing layouts
  • Fix dereferencing of arguments in treemapify() (#22, thanks @GregorDeCillia)
  • Fix deprecation of fill, label and group arguments in treemapify() to maintain backwards compatibility (thanks @GregorDeCillia)


Major changes

  • Refactor treemap layout algorithms
  • Add support for nested subgroups with new subgroup2 and subgroup3 aesthetics, and geom_treemap_subgroup2_border, geom_treemap_subgroup3_border, geom_treemap_subgroup2_text, and geom_treemap_subgroup3_text geoms.

Minor changes

  • Add 'hemisphere' column to G20 data

Bug fixes

  • Fix typo in README (#19, thanks @mruessler)


Minor changes

  • Change R and grid dependencies to 3.1
  • Remove reshape2 dependency

Bug fixes

  • Fix handling of data where some observations have area 0 (#17, thanks @py)


Minor changes

  • Documentation and misc. other changes in preparation for CRAN submission.
  • Refresh and update documentation.
  • Neater solution to import S3 method makeContent.fittexttree from 'ggfittext'.
  • Deprecate ggplotify()
  • Add travis-ci and CRAN badges to README
  • Misc. small changes in preparation for CRAN submission.

Bug fixes

  • Remove README.html so README renders correctly on GitHub
  • Fix arguments to geom_treemap_subgroup_text so they actually work.
  • Fix namespace incompatibility with ggfittext 0.4.2
  • Explicit invocation of all imported functions
  • Misc. small fixes in preparation for CRAN submission


Bug fixes

  • Remove wide characters causing problems in non-UTF-8 locales.


Major changes

  • Add ‘treemapify_fixed’ function
  • Add ‘fixed’ options to geoms
  • Add example of animated treemap to README


Minor changes

  • Fix ambiguity in README
  • Add test for inclusion of G20 data frame


Minor changes

  • Moved plyr and reshape2 from ‘Depends’ to ‘Imports’


Bug fixes

  • Fix required ggfittext version.
  • Fix typos.


Major changes

  • Add ‘reflow’ options to geom_treemap_text and geom_treemap_subgroup_text.
  • Add ‘lineheight’ aesthetic to geom_treemap_text and geom_treemap_subgroup_text.

Bug fixes

  • Fix namespace error causing text not to be drawn in some circumstances.


Major changes

  • Rename arguments for ggfittext v0.3.


Major changes

  • Added geom_treemap, geom_treemap_text, geom_treemap_subgroup_border and geom_treemap_subgroup_text ggplot2 geoms.


Bug fixes

  • Fix integer overflow error


Bug fixes

  • All font sizes now consistently and correctly expressed in points.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


2.5.5 by David Wilkins, a year ago


Report a bug at https://github.com/wilkox/treemapify/issues/

Browse source code at https://github.com/cran/treemapify

Authors: David Wilkins [aut, cre] , Bob Rudis [ctb]

Documentation:   PDF Manual  

GPL (>= 3) license

Imports ggplot2, ggfittext, grid

Suggests knitr, rmarkdown, testthat, vdiffr, spelling

Suggested by galah, nomisr.

See at CRAN