Skip to contents

Shows how a total is shared between items (browsers, energy sources, continents...) and how the shares change through time. A donut (or a pie) whose slices grow, shrink and smoothly slide past each other when the ranking changes is paired with a live league table: its rows slide to their new position and show the rank, a share bar, the share, the value and a green or red arrow when the item gained or lost places since the previous period. The centre of the donut shows the period and a total that counts smoothly. A player at the bottom plays, pauses and scrubs the animation.

Usage

donut_race(
  data,
  label,
  value,
  time,
  inner_radius = 0.6,
  sort = c("value", "fixed"),
  max_slices = NULL,
  other_label = "Other",
  table = TRUE,
  table_rows = NULL,
  labels = TRUE,
  center_label = "Total",
  fill = NULL,
  color_category = NULL,
  highlight = 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 slice per value).

value

The name of the numeric column giving the size of the slices. Negative, zero and missing values are left out (the item fades out).

time

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

inner_radius

The inner radius of the donut, as a fraction of the outer radius (from 0 to 0.9). Defaults to 0.6; 0 draws a pie.

sort

The order of the slices, clockwise from the top: "value" (default, the ranking of each period, largest first; the slices slide past each other when the ranking changes) or "fixed" (the order of the data, or of the factor levels, so that each slice stays in place and only grows or shrinks). The table is always ranked.

max_slices

Optional. The maximum number of slices: in each period, the smallest items are grouped into an "Other" slice so that at most max_slices slices are drawn.

other_label

The label of the grouped slice. Defaults to "Other".

table

Logical. Show the league table next to the donut (or under it when the chart is narrow). Defaults to TRUE.

table_rows

Optional. The maximum number of rows of the table. Defaults to as many as fit.

labels

Logical. Write the share on the slices that are large enough. Defaults to TRUE.

center_label

The caption under the total in the centre of the donut. Defaults to "Total"; FALSE hides the total.

fill

Optional. The colours of the items: a single colour, one colour per item (in order of appearance, recycled), or a named vector mapping colours to items (the palette is used for the others). By default, the first colours of the palette go to the largest items.

color_category

Optional. The name of a D3 categorical colour scheme, such as "Tableau10" or "Set2", used when fill is not given. Defaults to the ddplot palette.

highlight

Optional. A character vector of items pinned from the start.

agg

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

fill_gaps

Logical. Interpolate the missing values of an item between two periods where it is known. Defaults to TRUE.

frame_dur

The duration of one period of the animation, in milliseconds. Defaults to 900.

ease

The easing used inside each period, the name of a D3 easing function without the "ease" prefix, such as "Linear" or "CubicInOut" (default), see <https://github.com/d3/d3-ease>.

autoplay

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

loop

Logical. Start again at the end. Defaults to FALSE (it can be switched in the player).

controls

Logical. Show the player (play button, timeline, speed and loop buttons). Defaults to TRUE.

start_frame

Where the animation starts: "first" (default), "last" or a period number (1 for the first).

time_format

Optional. A D3 time format for date periods, such as "%b %Y". Defaults to a format chosen from the spacing of the periods.

title

Optional. The title of the chart.

title_font_size

The font size of the 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 of the chart. Defaults to "Verdana, Geneva, Tahoma, sans-serif".

bg_col

The background colour. Defaults to "white".

axis_col

The colour of the texts. Defaults to "#333333".

value_format

Optional. A D3 format string for the values, such as ",.0f" or ".2s".

value_prefix, value_suffix

Optional. Text added before and after the values, such as "$" or " TWh".

tooltip

Logical. Show a tooltip when hovering the slices and the rows. Defaults to TRUE.

width

Optional. The width of the chart.

height

Optional. The height of the chart.

Value

An interactive, animated SVG donut race (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). Rows with the same item and period are combined with agg. Gaps inside an item are interpolated; items missing before their first or after their last period grow in from zero and fade out. With max_slices, the smallest items of each period are grouped into an "Other" slice: items flow into it and out of it smoothly when they cross the cut, and the table still ranks every item.

Interactions

  • The player at the bottom plays and pauses the race (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 race.

  • Hover a slice or a table row: both are highlighted (the slice pulls out, the row is emphasised, the centre shows the share of the item) and a tooltip gives the value, the share, the rank and the change since the previous period. The tooltip keeps updating while the race plays. Hovering the "Other" slice lists the items it contains.

  • Click a slice or a row to pin it (it stays pulled out and highlighted in the table, the other slices are dimmed), click it again to unpin it, or click the background to unpin every item. highlight pins items from the start.

  • Alt-click (or shift-click) a row or a slice, or click the small cross at the end of a hovered row, to hide an item: the shares and the ranks are recomputed without it, and the slices close the gap smoothly. The "Show all" button at the top right brings the hidden items back.

  • The buttons at the top right switch the order of the slices between the ranking (largest first) and the fixed order of the data.

Shiny

Inside Shiny, with r2d3::d3Output("id") and r2d3::renderD3(), clicking a slice or a row sets input$id_clicked to a list with the label of the item, its value, share, rank, the period and whether it is now pinned. input$id_hidden gives the hidden items and input$id_frame the period shown (index and label). The current time, the speed, the pinned and hidden items and the slice order are kept when the chart is re-rendered.

Examples

library(gapminder)

# The world population by continent
donut_race(
  gapminder, label = "continent", value = "pop", time = "year",
  title = "World population by continent"
)
# The 7 most populous countries of each period, the others grouped donut_race( gapminder, label = "country", value = "pop", time = "year", max_slices = 8, inner_radius = 0.55, highlight = "India", title = "Who holds the world population?" )
# Character periods, a pie and a fixed order energy <- data.frame( source = rep(c("Coal", "Gas", "Nuclear", "Hydro", "Wind", "Solar"), 3), period = rep(c("2000s", "2010s", "2020s"), each = 6), twh = c(40, 20, 17, 16, 1, 0, 33, 23, 15, 16, 6, 2, 25, 24, 13, 15, 13, 10) ) donut_race( energy, label = "source", value = "twh", time = "period", inner_radius = 0, sort = "fixed", value_suffix = " %" )