Tutorial for generating treemaps with treemapify

This tutorial is adapted from the Treemapify documentation.

Dependencies and data

The Treemapify package depends on ggplot2, reshape2, plyr and must be installed with devtools

For any treemap, you need a data frame containing at least the following columns: * A numeric column, which will determine the area of each treemap rectangle. * Another numeric column, which will determine the fill color of each treemap rectangle.

For this example we’ll use a couple of optional columns * A factor column, containing labels for each rectangle (Country) * A second factor column, containing labels for groups of rectangles (Region)

require(devtools)
## Loading required package: devtools
# Install treemapify if not already
if(!require(treemapify)){install_github("wilkox/treemapify")}
## Loading required package: treemapify
## Loading required package: ggplot2
## Loading required package: plyr
## Loading required package: reshape2
# Load G20 summit data
data(G20)
head(G20)
##          Region        Country Trade.mil.USD Nom.GDP.mil.USD   HDI
## 1        Africa   South Africa        208000          384315 0.629
## 2 North America  United States       3969000        15684750 0.937
## 3 North America         Canada        962600         1819081 0.911
## 4 North America         Mexico        756800         1177116 0.775
## 5 South America         Brazil        494800         2395968 0.730
## 6 South America      Argentina        152690          474954 0.811
##   Population Economic.classification
## 1   53000000              Developing
## 2  316173000                Advanced
## 3   34088000                Advanced
## 4  112211789              Developing
## 5  201032714              Developing
## 6   40117096              Developing

Mapping the data to coordinates

The treemapify function generates coordinates for a treemap in which each observation is represented as a rectangle. In this example, each observation is a country; the rectangle’s area will be mapped to the country’s nominal GDP, while the fill color will be mapped to the Human Development Index (HDI). We’ll also group the contiries by region.

# Generate coordinates for the rectangles
treemap_coords <- treemapify(G20, area="Nom.GDP.mil.USD", fill="HDI", label="Country", group="Region")
head(treemap_coords)
##    fill           label     xmin     xmax     ymin      ymax         group
## 1 0.876  European Union  0.00000 38.66972  0.00000  58.99641        Europe
## 2 0.920         Germany 38.66972 63.32079  0.00000  19.17284        Europe
## 3 0.893          France 38.66972 63.32079 19.17284  33.88097        Europe
## 4 0.875  United Kingdom 38.66972 63.32079 33.88097  47.64081        Europe
## 5 0.881           Italy 38.66972 63.32079 47.64081  58.99641        Europe
## 6 0.937   United States  0.00000 53.16491 58.99641 100.00000 North America
ggplotify(treemap_coords) + labs(title="G-20 GDP and HDI")

In the example above, the fill color is mapped to HDI which is continous, giving us a continous color gradient. If we were to map the color to another variable (discrete) we would get a discrete colormapping.

treemap_coords <- treemapify(G20, area="Nom.GDP.mil.USD", fill="Economic.classification", group="Region", label="Country")
ggplotify(treemap_coords) + labs(title="G-20 GDP and Economic Classification")