Skip to contents

A treemap displays hierarchical (or flat) part-to-whole data as a set of nested rectangles whose areas are proportional to a value. The technique was invented by Ben Shneiderman in the early 1990s and was made famous by SmartMoney's "Map of the Market" and by the New York Times' graphics of the US federal budget and spending. When a group column is supplied the chart becomes a two-level hierarchy: tiles are coloured by group and each group gets its own header and border. Hovering over a tile highlights it and shows a tooltip with its label, group, value and share of the total.

Usage

treemap_chart(
  data,
  value,
  label,
  group = NULL,
  tile = "squarify",
  padding = 2,
  colors = NULL,
  valueFormat = ",.0f",
  showValues = TRUE,
  labelFontSize = 11,
  labelCol = NULL,
  groupFontSize = 12,
  strokeCol = "white",
  strokeWidth = 1,
  hover = TRUE,
  legend = TRUE,
  legendFontSize = 11,
  title = NULL,
  titleFontSize = 16,
  font = "Verdana, Geneva, Tahoma, sans-serif",
  bgcol = "white",
  textCol = "black",
  width = NULL,
  height = NULL
)

Arguments

data

The data frame containing the variables to consider.

value

The name of the numeric (non-negative) column that determines the area of each rectangle.

label

The name of the column used as the label of each rectangle (leaf).

group

Optional. The name of a categorical column used as the parent category of each rectangle. When supplied, rectangles are nested inside their group and coloured by group. Defaults to NULL.

tile

The tiling algorithm. One of "squarify" (default), "binary", "slice", "dice" or "sliceDice".

padding

Padding in pixels between rectangles (and around groups). Defaults to 2.

colors

Optional. A vector of colors. When group is supplied, one color is used per group; otherwise one color per rectangle. Colors are recycled if there are fewer colors than groups/rectangles. Defaults to NULL, which uses D3's Tableau 10 palette.

valueFormat

A d3-format specifier used to display values in the labels and tooltip (e.g. ",.0f", "$,.2s", ".3~s"). Defaults to ",.0f".

showValues

Whether to display the formatted value under each label. Defaults to TRUE.

labelFontSize

Font size of the rectangle labels. Labels are automatically truncated or hidden when a rectangle is too small to fit them. Defaults to 11.

labelCol

Optional. Color of the rectangle labels. Defaults to NULL, which picks white or near-black automatically for contrast with each rectangle's fill.

groupFontSize

Font size of the group headers (only used when group is supplied). Defaults to 12.

strokeCol

Color of the rectangle borders. Defaults to "white".

strokeWidth

Width of the rectangle borders in pixels. Defaults to 1.

hover

Whether to highlight the hovered rectangle and show a tooltip. Defaults to TRUE.

legend

Whether to display a legend of the groups (only used when group is supplied). Defaults to TRUE.

legendFontSize

Font size of the legend text. Defaults to 11.

title

Optional. Title of the chart.

titleFontSize

Font size of the chart title. Defaults to 16.

font

The font family used for all text. Defaults to "Verdana, Geneva, Tahoma, sans-serif".

bgcol

Background color of the SVG. Defaults to "white".

textCol

Color of the title and legend text. Defaults to "black".

width

Optional. The width of the SVG output.

height

Optional. The height of the SVG output.

Value

An SVG treemap chart.

Examples

# A toy government budget (in millions) by department and ministry
budget <- data.frame(
  department = c("Hospitals", "Primary care", "Public health",
                 "Schools", "Universities", "Vocational training",
                 "Army", "Navy", "Air force",
                 "Roads", "Railways", "Airports",
                 "Pensions", "Unemployment", "Family benefits"),
  ministry   = c("Health", "Health", "Health",
                 "Education", "Education", "Education",
                 "Defence", "Defence", "Defence",
                 "Transport", "Transport", "Transport",
                 "Social security", "Social security", "Social security"),
  spending   = c(5200, 2100, 800,
                 3900, 1800, 600,
                 2300, 1400, 1700,
                 1500, 1100, 400,
                 7400, 1900, 1300)
)

treemap_chart(
  data        = budget,
  value       = "spending",
  label       = "department",
  group       = "ministry",
  valueFormat = "$,.0f",
  title       = "Where the money goes (millions)"
)
# World population in 2007 by country, grouped by continent library(gapminder) pop_2007 <- gapminder[gapminder$year == 2007, ] treemap_chart( data = pop_2007, value = "pop", label = "country", group = "continent", valueFormat = ".3~s", padding = 1, title = "World population in 2007" )