help treemaptreemap v1.51 (GitHub)

treemap

: is a Stata package for plotting hierarchical data as a gridded tree map.

This program implements the squarify tiling algorithm (Bruls et. al. 2000).
The algorithm attempts to optimize the aspect ratio of rectangles relative to the overall graph dimensions.
Squarify is one of the most widely-used tiling method for treemaps. The Stata implementation is based on D3's treemap
and on the Python's squarify algorithms.

Syntax

treemap numvar [if] [in], by(variables (min=1 max=3))
[ xsize(num) ysize(num) format(str) share sformat(str) palette(it:str) colorby(name)
pad(list) labsize(list) linewidth(list) linecolor(list) fi(list) labcond(num)
addtitles novalues nolabels labsize(num) titlegap(num) labgap(str)
threshold(num) fade(num) labprop titleprop labscale(num) colorprop
title(str) subtitle(str) note(str) scheme(str) name(str) saving(str) ]

The options are described as follows:

treemap numvar The command requires a numeric variable that contains the values.

by(group vars) At least one by() string variable needs to be specified, and a maximum of three string variables are allowed. These also are used as labels.
The order is parent layer first followed by child layer or more aggregated layers should be specified first.

xsize(num), ysize(num) The width and height of the bounding box. Default values are xsize(5) and ysize(3).
Note that changing the xsize and ysize will change the layout of the treemap.

palette(str) Here one can use an named color scheme defined in the colorpalette package. Default is tableau.

colorby(name) The option allows us to preserve the color order by alphabetical order of the by() variables rather than values. This is useful if multiple
treemaps are drawn for the same data over time, and colors are likely to change across the same by() categories if their relative order changes.

share Show percentage shares.

addtitles Add titles to rectangles of higher layers. This adds the name and value in the top left corner of the boxes.

novalues Do not add the values to the lowest-level rectangles. If the graph is too crowded, this option might help.

nolabels Do not add any labels. This gives just boxes without any numbers. This option overrides the above two options.

Fine tuning

threshold(num) The value below which categories are combined in a "Rest of ..." category. Default is thresh(0).

labcond(value) The minimum value for showing the value labels. For example, labcond(20) will only plot values greater than 20. If noval is specified
in combination with percent then the threshold will use the percentage values.

format(fmt) Format the values. The default option is format(%12.0fc).

sformat(fmt) Format the percentage shares. The default option is sformat(%5.2f).

pad(numlist max=3) The padding of the boxes, which can be defined as a list. The default values are pad(0.012 0.01 0.01) for the three layers. A value of 0
implies no padding. If you change the xsize and ysize substantially, then you might also need to update the padding.

labsize(string max=3) The size of the labels. The default values are labs(1.6 1.6 1.6). If only one value is specified, it will passed on to all the layers.

linewidth(string max=3) The line width of the boxes. The default values are linew(0.03 0.03 0.03). If only one value is specified, it will passed on to all the layers.

linecolor(string max=3) The line color of the boxes. The default values are linec(black black black). If only one value is specified, it will passed on to all the layers.

fi(numlist max=3) The fill intensity of the layers in the other they are specified. The default values are fi(50 75 100).

titlegap(num) Change the space between the title text and the boxes. Default value is titlegap(0.1).

labgap(num) Change the space between the box text and the values. Default value is labgap(0.6). This option might be use if labelprop is used which might make
some labels overlap with each other.

titleprop Make the size of the box titles proportional to the area.

labprop Make the size of the labels proportional to the area.

colorprop Add color gradient to the box colors. The colors are interpolated from the assigned level color to a 10% value of the color.
This can be changed using the fade() described below.

fade(num) Change the end color used for interpolation in the colorprop. Default value is fade(10) or 10% of the color.

labscale(num) This option changes how the labels are scaled. This is an advanced option and should be used cautiously. Default value is labscale(0.3333).
The formula for scaling is ((height x width x area) / sum of values)^labscale.

title(), subtitle(), note() These are standard twoway graph options.

name(), saving() These are standard twoway graph options.

scheme(string) Load the custom scheme. Above options can be used to fine tune individual elements.

Dependencies

The palette package (Jann 2018, 2022) is required for treemap:

ssc install palettes, replace
ssc install colrspace, replace

Even if you have these installed, it is highly recommended to check for updates: ado update, update

Examples

See GitHub for a comprehensive set of examples. Basic use below:

- use "https://github.com/asjadnaqvi/stata-treemap/blob/main/data/demo_r_pjangrp3_clean.dta?raw=true", clear

- drop year
- keep NUTS_ID y_TOT
- drop if y_TOT==0
- keep if length(NUTS_ID)==5

- gen NUTS2 = substr(NUTS_ID, 1, 4)
- gen NUTS1 = substr(NUTS_ID, 1, 3)
- gen NUTS0 = substr(NUTS_ID, 1, 2)
- ren NUTS_ID NUTS3

- treemap y_TOT, by(NUTS0)

- treemap y_TOT, by(NUTS0) addtitles labsize(2) format(%15.0fc)

- treemap y_TOT if NUTS0=="AT", by(NUTS2 NUTS3) addtitles noval labsize(1.6) format(%15.0fc)

Feedback

Please submit bugs, errors, feature requests on GitHub by opening a new issue.

Package details

Version : treemap v1.51
This release : 24 Oct 2023
First release: 08 Sep 2022
Repository : GitHub
Keywords : Stata, graph, treemap, squarify
License : MIT

Author : Asjad Naqvi
E-mail : asjadnaqvi@gmail.com
Twitter : @AsjadNaqvi

References

Bruls, M., Huizing, K., van Wijk Jarke J. (2000). Squarified Treemaps. Data Visualization 2000, Eurographics.

Bostock, M. (2022). D3 Treemap. Observable HQ.

Jann, B. (2018). Color palettes for Stata graphics. The Stata Journal 18(4): 765-785.

Jann, B. (2022). Color palettes for Stata graphics: an update. University of Bern Social Sciences Working Papers No. 43.

Laserson, U. (2022). Python squarify.

Other visualization packages

arcplot, alluvial, bimap, bumparea, bumpline, circlebar, circlepack, clipgeo, delaunay, joyplot,
marimekko, sankey, schemepack, spider, streamplot, sunburst, treecluster, treemap