library(gapminder)
eu <- as.data.frame(gapminder[gapminder$continent == "Europe", ])
top5 <- do.call(rbind, lapply(split(eu, eu$year), function(d) {
d <- head(d[order(-d$gdpPercap), ], 5)
d$rank <- 1:5
d
}))
table(top5$country)
#>
#> Afghanistan Albania Algeria
#> 0 0 0
#> Angola Argentina Australia
#> 0 0 0
#> Austria Bahrain Bangladesh
#> 3 0 0
#> Belgium Benin Bolivia
#> 0 0 0
#> Bosnia and Herzegovina Botswana Brazil
#> 0 0 0
#> Bulgaria Burkina Faso Burundi
#> 0 0 0
#> Cambodia Cameroon Canada
#> 0 0 0
#> Central African Republic Chad Chile
#> 0 0 0
#> China Colombia Comoros
#> 0 0 0
#> Congo, Dem. Rep. Congo, Rep. Costa Rica
#> 0 0 0
#> Cote d'Ivoire Croatia Cuba
#> 0 0 0
#> Czech Republic Denmark Djibouti
#> 0 9 0
#> Dominican Republic Ecuador Egypt
#> 0 0 0
#> El Salvador Equatorial Guinea Eritrea
#> 0 0 0
#> Ethiopia Finland France
#> 0 0 0
#> Gabon Gambia Germany
#> 0 0 6
#> Ghana Greece Guatemala
#> 0 0 0
#> Guinea Guinea-Bissau Haiti
#> 0 0 0
#> Honduras Hong Kong, China Hungary
#> 0 0 0
#> Iceland India Indonesia
#> 3 0 0
#> Iran Iraq Ireland
#> 0 0 2
#> Israel Italy Jamaica
#> 0 0 0
#> Japan Jordan Kenya
#> 0 0 0
#> Korea, Dem. Rep. Korea, Rep. Kuwait
#> 0 0 0
#> Lebanon Lesotho Liberia
#> 0 0 0
#> Libya Madagascar Malawi
#> 0 0 0
#> Malaysia Mali Mauritania
#> 0 0 0
#> Mauritius Mexico Mongolia
#> 0 0 0
#> Montenegro Morocco Mozambique
#> 0 0 0
#> Myanmar Namibia Nepal
#> 0 0 0
#> Netherlands New Zealand Nicaragua
#> 10 0 0
#> Niger Nigeria Norway
#> 0 0 12
#> Oman Pakistan Panama
#> 0 0 0
#> Paraguay Peru Philippines
#> 0 0 0
#> Poland Portugal Puerto Rico
#> 0 0 0
#> Reunion Romania Rwanda
#> 0 0 0
#> Sao Tome and Principe Saudi Arabia Senegal
#> 0 0 0
#> Serbia Sierra Leone Singapore
#> 0 0 0
#> Slovak Republic Slovenia Somalia
#> 0 0 0
#> South Africa Spain Sri Lanka
#> 0 0 0
#> Sudan Swaziland Sweden
#> 0 0 1
#> Switzerland Syria Taiwan
#> 12 0 0
#> Tanzania Thailand Togo
#> 0 0 0
#> Trinidad and Tobago Tunisia Turkey
#> 0 0 0
#> Uganda United Kingdom United States
#> 0 2 0
#> Uruguay Venezuela Vietnam
#> 0 0 0
#> West Bank and Gaza Yemen, Rep. Zambia
#> 0 0 0
#> Zimbabwe
#> 0A top five that changes members
The bar chart race follows the same ten countries through every year. A real league table is less tidy: the top five of 1952 is not the top five of 2007, so some bars have nowhere to come from and others nowhere to go. The tween handles both cases with one rule. A bar found in two adjacent years moves from one to the other; a bar found in only one of them fades in or out on the spot.
Ten countries pass through Europe’s top five by GDP per capita over the twelve survey years. Norway and Switzerland are in every year; Sweden appears once.
a <- vplot(top5, width = 7, height = 3.6) |>
mark_bar(x = 6 - rank, y = gdpPercap, fill = country) |>
mark_text(x = 6 - rank, y = gdpPercap + 600, label = country,
hjust = 0, size = 10) |>
coord_flip() |>
guides(fill = "none") |>
scale_y_continuous(limits = c(0, 60000)) |>
theme(axis.text.y = element_blank(), axis.ticks.y = element_blank()) |>
labs(title = "Europe's five highest GDPs per capita, {closest_state}",
x = NULL, y = "GDP per capita (1990 dollars)") |>
transition_states(year, transition_length = 2, state_length = 1, wrap = FALSE) |>
animate(nframes = 330, fps = 25)Watch the bottom slots. The United Kingdom is third in 1952 and gone after 1957. Germany comes in in 1962, drops out for Sweden’s single year in 1967 and is back in 1972. On the move from 1967 to 1972 the leaver and the newcomer hold the same slot, fifth, so for the length of the move two bars share it, one fading out while the other fades in. Iceland and Austria come and go in the 1980s and 1990s, and Ireland arrives in third place in 2002 and climbs to second.
How bars are matched
Each bar is keyed by its group, and with no group or data_id the group is the combination of the layer’s discrete mappings, here fill = country (the labels are keyed by label = country). So the engine looks for the same country in the next year:
- found: the bar slides from its old rank to its new one and its length tweens;
- missing from the next year: the bar fades out where it is;
- missing from the previous year: the bar fades in at its new place.
The fade runs on the alpha easing, so ease_aes(alpha = ...) shapes it separately from the movement. Because the scales are trained once over all the years, a country that enters gets the same colour and is measured against the same axis as everyone else.
One detail in the data matters: the axis is extended to 60 000 so the labels to the right of the longest bar stay inside the panel. eu$country can stay a factor: the colour scale drops the levels no row uses, as in ggplot2, so it only knows the ten countries that appear rather than all 142 levels of gapminder$country.