library(gapminder)
g <- transform(as.data.frame(gapminder), gdp = pop * gdpPercap / 1e9)
biggest <- head(g[g$year == 2007, ][order(-g$gdp[g$year == 2007]), "country"], 10)
d <- g[g$country %in% biggest, c("country", "year", "gdp")]
d$country <- droplevels(d$country)
d <- do.call(rbind, lapply(split(d, d$year), function(s) {
s$rank <- rank(s$gdp, ties.method = "first")
s[order(-s$rank), ] # rows in rank order: the tween does not care
}))
head(d[d$year == 1952, ], 3)
#> country year gdp rank
#> 1952.1609 United States 1952 2204.2424 10
#> 1952.1597 United Kingdom 1952 503.2666 9
#> 1952.565 Germany 1952 493.9866 8A bar chart race, with no new API
A bar chart race is a ranking that reorders itself over time. There is no mark_race(): it falls out of mark_bar(), coord_flip() and transition_states(), because the tween engine matches each bar to the same country in the next state and interpolates its position as well as its length. When two countries swap rank, their bars slide past each other.
The one thing the data has to carry is the rank. Bar position comes from the x encoding, so ranking within each year and mapping x = rank is what makes bars move; map x = country instead and you get twelve years of bars growing in place.
Ten countries, twelve five-year steps, and a rank from 1 (smallest) to 10 (largest) recomputed inside each year. The rows of each year are in rank order, so a country sits on a different row from one year to the next. That does not matter: a bar’s identity is its group, and with no group or data_id given, the group is the combination of the layer’s discrete mappings, here fill = country. The text layer is keyed the same way, by its discrete label = country.
a <- vplot(d, width = 7, height = 4.5, dpi = 150) |>
mark_bar(x = rank, y = gdp, fill = country) |>
mark_text(x = rank, y = gdp + 150, label = country, hjust = 0, size = 10) |>
coord_flip() |>
guides(fill = "none") |>
scale_y_continuous(limits = c(0, 16000)) |>
theme(axis.text.y = element_blank(), axis.ticks.y = element_blank()) |>
labs(title = "Total GDP of the ten largest economies of 2007, in {closest_state}",
x = NULL, y = "billions of 1990 dollars") |>
transition_states(year, transition_length = 2, state_length = 1, wrap = FALSE) |>
animate(nframes = 330, fps = 25)China starts seventh of the ten and takes second place off Japan in 2002. Japan had climbed to second in 1972 and held it until then. India sits eighth as late as 1997, then passes Italy, France, the United Kingdom and Germany to reach fourth by 2007. The United States never moves. Labels ride their bars, so you can follow one country through the reshuffling instead of re-reading the axis every time the order changes.
What the engine is doing
Bars interpolate on two channels at once. Length follows gdp, and position follows rank, so a country whose rank changes by one slides one slot while its bar grows. The scales were trained once across all twelve years and frozen, which is why the axis never jumps: 1952 is drawn against the same 16000-billion axis as 2007, and the growth is legible as growth rather than as a rescaling.
transition_length and state_length set the rhythm. With 2:1 and wrap = FALSE, the 330 frames give each of the eleven moves about 20 frames and hold each year for about 10, so the overtakes are visible and the ranking is readable at rest. wrap = FALSE ends the animation on 2007 instead of adding a twelfth move that slides every bar back to its 1952 place. The file still loops, so playback cuts from 2007 straight back to 1952 and starts again.
The year in the title is a frame variable. labs() text may embed {...} expressions, glue-style, evaluated on every frame: {closest_state} is the year the frame is nearest to, and it flips halfway through each move, at the same moment the labels would if their text changed. {previous_state}, {next_state}, {transitioning}, {frame} and {progress} are there too.
Format
The output is an animated SVG. Ten bars, ten labels and an axis are a light scene, so 330 frames come to under 3 MB of markup, and because SVG is text it travels compressed: under 100 KB over the wire. The same animation as a GIF is around 10 MB. A GIF is still the choice when the animation must go somewhere that only takes images, such as a slide or a chat message: anim_save("race.gif", a).