Skip to contents

Draws one bubble per item (id) at its x and y values, sized by size and coloured by group, and animates the bubbles smoothly from one period (time) to the next, in the style of the famous Gapminder chart by Hans Rosling. The axes are fixed over the whole race so that every move means something, the current period is drawn as a large faded watermark behind the bubbles, and items that appear or disappear fade in and out. Periods can be numbers (years), dates or characters / factors.

Usage

bubble_race(
  data,
  x,
  y,
  id,
  time,
  size = NULL,
  group = NULL,
  x_scale = c("linear", "log"),
  y_scale = c("linear", "log"),
  size_range = c(3, 40),
  fill = NULL,
  color_category = NULL,
  opacity = 0.85,
  highlight = NULL,
  trails = FALSE,
  labels = 5,
  legend = NULL,
  size_legend = TRUE,
  buttons = TRUE,
  x_title = NULL,
  y_title = NULL,
  size_title = NULL,
  agg = c("mean", "sum", "max", "min", "last"),
  fill_gaps = TRUE,
  frame_dur = 1000,
  ease = "Linear",
  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",
  grid_col = "#ebebeb",
  value_format = NULL,
  value_prefix = "",
  value_suffix = "",
  tooltip = TRUE,
  width = NULL,
  height = NULL
)

Arguments

data

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

x

The name of the numeric column drawn on the horizontal axis.

y

The name of the numeric column drawn on the vertical axis.

id

The name of the column identifying the items (one bubble per value).

time

The name of the column giving the period: numeric (e.g. years), Date, POSIXct, factor or character (sorted, or in the order of the factor levels).

size

Optional. The name of a numeric, non-negative column giving the size of the bubbles (their area is proportional to it). Without it, every bubble has the same radius (7 pixels, or size_range[1] if larger).

group

Optional. The name of a column grouping the items (e.g. a continent). It sets the colours and adds a clickable legend.

x_scale, y_scale

The scales of the axes: "linear" (default) or "log". On a log scale, values that are not positive are dropped (with a warning). Both can be switched from the chart.

size_range

A numeric vector of length 2: the smallest and the largest bubble radius in pixels (the largest is reduced on small charts). Defaults to c(3, 40).

fill

Optional. The colours of the bubbles. With group, one colour per group (a named vector maps colours to groups). Without group, a single colour for every bubble, or one colour per item (recycled; a named vector maps colours to items). Defaults to a built-in palette.

color_category

Optional. The name of a D3 categorical colour scheme used instead of the built-in palette, e.g. "Tableau10", "Set2" or "Dark2". Ignored when fill is given.

opacity

The opacity of the bubbles, from 0 to 1. Defaults to 0.85.

highlight

Optional. A character vector of items to follow from the start (their trails and labels are drawn, the others are dimmed).

trails

Logical. Draw the trail of every bubble (best with a few items). It can be switched from the chart. Defaults to FALSE.

labels

The number of bubbles labelled with their name: the largest ones (by size, or by y without size). Use 0 or FALSE for none and TRUE for all (labels that would overlap are skipped). Followed bubbles are always labelled; while some bubbles are followed, only they are labelled. Defaults to 5.

legend

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

size_legend

Logical. Show a legend of reference circles for size, drawn in the emptiest corner of the plot. Defaults to TRUE.

buttons

Logical. Show the buttons at the top right (log scales, trails, clear). Defaults to TRUE.

x_title, y_title

Optional. The titles of the axes. They default to the names of x and y; use "" to hide them.

size_title

Optional. The title of the size legend and the name of the size in the tooltip. Defaults to the name of size.

agg

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

fill_gaps

Logical. Interpolate the values of an item between the periods where it is known, so that it does not blink out in a gap. Defaults to TRUE.

frame_dur

The duration of one period, in milliseconds. Defaults to 1000.

ease

The easing used between two periods, the name of a D3 easing function without the "ease" prefix: "Linear" (default, a smooth continuous motion), "CubicInOut" (a short pause at each period), "QuadInOut", ...

autoplay

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

loop

Logical. Start again at the end. It can be switched from the player. Defaults to FALSE.

controls

Logical. Show the player (play / pause, timeline, speed, loop) at the bottom. Defaults to TRUE.

start_frame

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

time_format

Optional. A d3-time-format string used for Date and POSIXct periods, e.g. "%b %Y". Chosen from the spacing of the periods by default.

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

bg_col

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

axis_col

The colour of the axes, labels and titles. Defaults to "#333333".

grid_col

The colour of the grid lines. Defaults to "#ebebeb"; use NA to hide them.

value_format

Optional. A d3-format string for the values, e.g. ",.0f" or ".3s". A single string applies to x, y and size; a named vector sets them one by one, e.g. c(x = "$,.0f", y = ".1f").

value_prefix, value_suffix

Optional. Text added before or after the values, e.g. "$" or " years". As for value_format, a single string applies to every value and a named vector sets them one by one, e.g. value_prefix = c(x = "$").

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, interactive SVG bubble chart (an r2d3 htmlwidget).

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 first and last period; speed and loop buttons sit on the right.

  • Hover a bubble to highlight it: guides are drawn to both axes with the values on the axes, and a tooltip lists its group, its values and their change over the last period. The tooltip stays on the bubble and keeps updating while the race plays.

  • Click a bubble to follow it: its trail (its path through every period so far, with a dot per period) is drawn, its label stays on, and the other bubbles are dimmed. Click it again to stop following it. Several bubbles can be followed. Hover a trail dot to see the values of that period, click it to jump to that period. Escape stops following every bubble.

  • The buttons at the top right switch the x and y axes between linear and log scales (when the values are all positive), show the trails of every bubble, and clear the followed bubbles.

  • Click a legend entry to hide or show a group, double-click it to isolate it. Hovering a legend entry highlights the group.

Shiny

Inside Shiny, with r2d3::d3Output("id") and r2d3::renderD3(), clicking a bubble sets input$id_clicked to a list with the id, group, x, y, size and period of the bubble, input$id_followed gives the followed items, and input$id_frame gives the current period (a list with its index and label). The current period, the followed items, the hidden groups, the scales and the trails mode are kept when the chart is re-rendered.

Examples

library(gapminder) # needed for the gapminder data frame

bubble_race(
  gapminder,
  x = "gdpPercap", y = "lifeExp", id = "country", time = "year",
  size = "pop", group = "continent",
  x_scale = "log",
  highlight = c("China", "India"),
  x_title = "GDP per capita", y_title = "Life expectancy",
  size_title = "Population",
  value_prefix = c(x = "$"), value_suffix = c(y = " years"),
  value_format = c(x = ",.0f", y = ".1f"),
  title = "Health and wealth of nations"
)
# A few items with their trails library(dplyr) # needed for data wrangling gapminder %>% filter(country %in% c("China", "India", "Japan", "Brazil", "Nigeria")) %>% bubble_race( x = "gdpPercap", y = "lifeExp", id = "country", time = "year", size = "pop", x_scale = "log", trails = TRUE, labels = TRUE, title = "Five countries, 1952 to 2007" )