Skip to contents

Draws one tile per item (countries, products, companies...) with an area proportional to its value, and animates the treemap through time: the tiles grow, shrink and reflow smoothly as the values change, items that enter fade and grow in from nothing, and leaving ones shrink away. Think "share of the world population" or "market capitalisation" through the years. A player at the bottom plays, pauses and scrubs the animation and a big period label sits in the top right corner.

Usage

treemap_race(
  data,
  label,
  value,
  time,
  group = NULL,
  tile = c("resquarify", "squarify", "binary", "slice", "dice", "sliceDice"),
  fill = NULL,
  color_category = NULL,
  color_by = c("group", "growth"),
  highlight = NULL,
  labels = TRUE,
  label_mode = c("value", "share", "growth"),
  padding = 2,
  max_label_size = 26,
  legend = NULL,
  agg = c("sum", "mean", "max", "min", "last"),
  fill_gaps = TRUE,
  frame_dur = 900,
  ease = "CubicInOut",
  autoplay = TRUE,
  loop = FALSE,
  controls = TRUE,
  start_frame = "first",
  time_format = NULL,
  title = NULL,
  title_font_size = 20,
  subtitle = NULL,
  subtitle_font_size = 13,
  font = "Verdana, Geneva, Tahoma, sans-serif",
  bg_col = "white",
  axis_col = "#333333",
  value_format = NULL,
  value_prefix = "",
  value_suffix = "",
  tooltip = TRUE,
  width = NULL,
  height = NULL
)

Arguments

data

The data frame containing the variables to consider (one row per item and period).

label

The name of the column giving the items (one tile per value).

value

The name of the numeric column giving the area of the tiles. Negative values cannot be drawn and are treated as missing.

time

The name of the column giving the periods: numeric (e.g. years), Date, POSIXct, or a factor or character column (factor levels, or the sorted values, give the order).

group

Optional. The name of a column grouping the items (e.g. the continent of each country). The treemap then has two levels: each group gets a frame, a header and a colour, and the legend can hide groups.

tile

The tiling algorithm: "resquarify" (default, squarified tiles that keep their place while the values change), "squarify" (the best squarified layout of every period, morphed from one period to the next), "binary" (the same with a balanced binary split), "slice" (vertical strips), "dice" (horizontal strips) or "sliceDice" (strips alternating with the depth). The strips keep the order of the items by average value.

fill

Optional. The colours of the tiles. With group, one colour per group (recycled, or a named vector mapping colours to groups); the items are drawn in shades of their group colour. Without group, one colour per item (recycled, or a named vector mapping colours to items). Defaults to the ddplot palette, the largest groups or items getting the first colours.

color_category

Optional. The name of a D3 categorical colour scheme (e.g. "Tableau10", "Set2"), see <https://github.com/d3/d3-scale-chromatic#categorical>, used when fill is not given.

color_by

How the tiles are coloured at first: "group" (default, the palette: by group, or by item without group) or "growth" (a heat map of the growth since the previous period, red for shrinking items, green for growing ones). It can be changed from the chart when controls = TRUE.

highlight

Optional. A character vector of items pinned from the start (outlined, the other tiles dimmed).

labels

Logical. Write the names and values in the tiles (labels that do not fit are hidden, their font size follows the tile size). Defaults to TRUE.

label_mode

What the labels show under the names at first: "value" (default), "share" (share of the total, or of the group when zoomed in) or "growth" (change since the previous period). It can be changed from the chart when controls = TRUE.

padding

The space between tiles, in pixels. Defaults to 2.

max_label_size

The largest font size of the tile labels, in pixels. Defaults to 26.

legend

Logical. Show the legend of the groups. When NULL (default), it is shown when group is used.

agg

How rows with the same item and period are combined: "sum" (default), "mean", "max", "min" or "last".

fill_gaps

Logical. Interpolate the value of an item between the periods where it is known. Defaults to TRUE; when FALSE, the tile shrinks away during the missing periods.

frame_dur

The duration of one period, in milliseconds, at 1x speed. Defaults to 900.

ease

The easing used within each period, a d3-ease name such as "Linear" (a steady animation) or "CubicInOut" (default, slows down on every period). See <https://github.com/d3/d3-ease>.

autoplay

Logical. Start playing when the chart scrolls into view. Defaults to TRUE.

loop

Logical. Start again from the first period at the end. Defaults to FALSE (it can be changed from the player).

controls

Logical. Show the player at the bottom and the label and colour buttons at the top. Defaults to TRUE.

start_frame

The period shown first: "first" (default), "last" or a period number (1 for the first one).

time_format

Optional. A d3-time-format string used for dates, e.g. "%b %Y". Chosen from the spacing of the periods when NULL.

title

Optional. The title of the plot.

title_font_size

The font size of the plot title. Defaults to 20.

subtitle

Optional. A subtitle drawn under the title.

subtitle_font_size

The font size of the subtitle. Defaults to 13.

font

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

bg_col

The background colour of the SVG. Defaults to "white".

axis_col

The colour of the titles, headers and controls. Defaults to "#333333".

value_format

Optional. A d3-format string for the values, e.g. ",.0f", ".1f" or ".3~s". Compact values ("1.23M") are used when NULL.

value_prefix, value_suffix

Optional. Text added before or after every value, e.g. "$" or " t".

tooltip

Logical. Show a tooltip on hover. Defaults to TRUE.

width

Optional. The width of the SVG output.

height

Optional. The height of the SVG output.

Value

An animated SVG treemap (an r2d3 htmlwidget).

Details

The data is long: one row per item and period, with the item names in label, the values in value and the periods in time (numbers such as years, dates, or a factor or character column whose levels give the order). Gaps inside an item are interpolated; items missing before their first or after their last period enter and leave the treemap. With group, the treemap has two levels (e.g. continent and country): each group gets a frame and a header with its total, and its tiles are drawn in shades of the group colour.

With the default tile = "resquarify", the layout keeps the same rows of tiles while the values change, so the tiles glide instead of jumping. When a row gets too stretched, the treemap is re-squarified, with a short morph from the old layout to the new one.

Interactions

  • The player at the bottom plays and pauses the animation (also with the space bar), the timeline can be dragged or clicked, the arrow keys step one period, Home and End jump to the start and the end. The speed button cycles from 0.5x to 4x, the loop button repeats the animation.

  • Hover a tile to see its value, its share of the total and of its group, its rank and its change since the previous period. The tooltip keeps updating while the animation plays. Hover a group header to see the group total and its largest items.

  • Click a group header to zoom into the group: its treemap fills the chart and keeps playing. Click "All" in the header (or press Escape) to zoom out.

  • Click a tile to pin it: it gets an outline and the other tiles are dimmed. Click it again to unpin it. highlight pins items from the start.

  • The buttons at the top switch the tile labels between the value, the share of the total and the growth since the previous period, and the colours between the palette and a growth heat map (red for items that shrink, green for items that grow).

  • With group, click a legend entry to hide or show a group (the treemap is recomputed without it), double-click it to isolate it, hover it to highlight its tiles.

Shiny

Inside Shiny, with r2d3::d3Output("id") and r2d3::renderD3(), clicking a tile sets input$id_clicked to a list with the item, its group, value, share, period and whether it is now pinned; input$id_zoom gives the group zoomed into (NULL when zoomed out) and input$id_frame the period shown (index and label). The current time, the speed, the pinned items, the hidden groups, the zoom and the label and colour modes are kept when the chart is re-rendered.

Examples

library(gapminder)

# World population by country, grouped by continent
treemap_race(
  gapminder,
  label = "country",
  value = "pop",
  time = "year",
  group = "continent",
  title = "World population",
  subtitle = "Click a continent header to zoom in",
  highlight = c("China", "India")
)
# A few items, without groups, coloured by growth library(dplyr) # needed for data wrangling gapminder %>% filter(country %in% c("France", "Germany", "Italy", "Spain", "Poland")) %>% mutate(gdp = gdpPercap * pop / 1e9) %>% treemap_race( label = "country", value = "gdp", time = "year", color_by = "growth", label_mode = "share", value_prefix = "$", value_suffix = "B", value_format = ",.0f", title = "GDP of five European countries" )