Skip to contents

A circular bar chart race: at every period, the top top_n items are drawn around a central circle, sorted by value clockwise from the top, and they glide to their new place when their rank changes. Two layouts are available: "spokes", where each item is a bar radiating from the inner circle (its length is the value), and "rings", where each item is a concentric arc (the outermost ring is the leader, the length of the arc is the value). The centre shows the period and the total of the items on show. The values are interpolated between periods, so playing and scrubbing are smooth; items entering or leaving the top slide in from, or out to, the gap at the top of the circle.

Usage

radial_bar_race(
  data,
  label,
  value,
  time,
  group = NULL,
  top_n = 12,
  layout = c("spokes", "rings"),
  inner_radius = 0.25,
  scale = c("linear", "area"),
  max = NULL,
  agg = c("sum", "mean", "max", "min", "last"),
  fill_gaps = TRUE,
  fill = NULL,
  color_category = NULL,
  highlight = NULL,
  center_value = c("total", "average", "none"),
  legend = NULL,
  layout_buttons = 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

A data frame in long format: one row per item and period.

label

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

value

The name of the numeric column to race. Negative values are drawn as zero.

time

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

group

Optional. The name of a column grouping the items (e.g. a continent): the bars are then coloured by group, with a clickable legend.

top_n

The number of items on show at every period. Defaults to 12.

layout

The initial layout: "spokes" (default, bars radiating from the centre) or "rings" (concentric arcs, best with top_n up to about 12). It can be changed from the chart.

inner_radius

The radius of the central circle: a number below 1 is a fraction of the radius of the chart, a larger number is a radius in pixels. Defaults to 0.25.

scale

How the values are mapped to the length of the spokes: "linear" (default, the length from the inner circle is proportional to the value) or "area" (the area of each spoke is proportional to the value).

max

Optional. A fixed maximum value for the scale (e.g. 100 for percentages). By default the scale follows the leader of each period, smoothly.

agg

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

fill_gaps

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

fill

Optional. The colours: a single colour, a vector of colours (recycled), or a named vector mapping colours to items (or to groups, with group). Defaults to the ddplot palette.

color_category

Optional. The name of a D3 categorical colour scheme, such as "Tableau10", "Set2" or "Paired", used when fill is not given.

highlight

Optional. A character vector of items to pin from the start.

center_value

What the centre shows under the period: "total" (default, the total of the items on show), "average" (their average, better for rates such as a life expectancy) or "none".

legend

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

layout_buttons

Logical. Show the Spokes / Rings buttons. Defaults to TRUE.

frame_dur

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

ease

The easing used within a period, the name of a D3 easing function without the "ease" prefix, e.g. "CubicInOut" (default), "Linear" or "QuadInOut".

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 toggled from the player.

controls

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

start_frame

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

time_format

Optional. A d3-time-format string for date periods, e.g. "%b %Y".

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

bg_col

The background colour. Defaults to "white".

axis_col

The colour of the texts, grid and controls. Defaults to "#333333".

value_format

Optional. A d3-format string for the values, e.g. ",.0f", ".1f" or "~s".

value_prefix, value_suffix

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

tooltip

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

width

Optional. The width of the chart.

height

Optional. The height of the chart.

Value

An animated radial bar chart race (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 periods. The speed (0.5x to 4x) and loop buttons sit on the right.

  • The Spokes / Rings buttons at the top right morph the chart from one layout to the other, even while it plays.

  • Hover a bar to highlight it and see a live tooltip with its value, its rank, its change since the previous period and its share of the total; the centre then shows the hovered item.

  • Click a bar to pin it (click again to unpin): pinned items stay highlighted and the centre follows the last pinned item, even when it drops out of the top. highlight pins items from the start.

  • Small arrows next to the labels show the items that just moved up or down.

  • With group, click a legend entry to hide or show a group, double-click it to keep only that group; hovering an entry highlights its items. The ranks are computed without the hidden groups.

Shiny

Inside Shiny, with r2d3::d3Output("id") and r2d3::renderD3(), clicking a bar sets input$id_clicked to a list with the label, group, rank, value, period and pinned state of the item, and input$id_frame gives the current period (a list with its index and label). The time, the layout, the pinned items and the hidden groups are kept when the chart is re-rendered.

Examples

library(gapminder)

radial_bar_race(
  gapminder,
  label = "country",
  time = "year",
  value = "pop",
  title = "The most populous countries",
  subtitle = "Population, 1952 to 2007"
)
# Coloured by continent, as concentric rings radial_bar_race( gapminder, label = "country", time = "year", value = "gdpPercap", group = "continent", top_n = 10, layout = "rings", value_prefix = "$", value_format = ",.0f", title = "GDP per capita" )
# Character periods and a fixed maximum scores <- data.frame( team = rep(c("Red", "Blue", "Green", "Gold", "Teal"), times = 4), round = rep(c("Round 1", "Round 2", "Round 3", "Round 4"), each = 5), points = c(10, 25, 18, 7, 30, 35, 40, 22, 30, 41, 60, 52, 47, 49, 55, 80, 70, 76, 61, 66) ) radial_bar_race(scores, label = "team", time = "round", value = "points", max = 100, frame_dur = 1400)