library(gapminder)
a <- vplot(transform(gapminder, pop = pop / 1e6), width = 7, height = 4.5) |>
mark_point(x = gdpPercap, y = lifeExp, size = pop, color = continent,
data_id = country, alpha = 0.8) |>
scale_x_continuous(trans = "log10") |>
labs(title = "Year: {round(frame_time)}", x = "GDP per capita",
y = "Life expectancy", size = "population (millions)") |>
transition_time(year) |>
animate(nframes = 120, fps = 20)Animated bubble chart (gapminder)
vellumplot animates a plot by compiling one keyframe per state and tweening the frames between them. The scales are trained once over every state and frozen, so the axes, the colour key and the size legend stay put while the data moves. The animation is non-reactive: the states are fixed up front and nothing retrains.
transition_time() is the idiom for a continuous time like year: it allocates frames in proportion to the gaps between years and never pauses or wraps, so the bubbles glide at a constant rate. data_id = country tells the tween which bubble is which: each country moves into its own position in the next year, whatever order the rows come in. The title is a template, and {round(frame_time)} is evaluated on every frame, so the year counts up between keyframes in step with the bubbles.
anim_save("gapminder.svg", a)Each bubble’s position and size interpolate frame to frame. Because the x, y and size scales were frozen over the union of all years, a country that is poor early and rich late keeps one stable frame of reference throughout: China and India, the two large green bubbles, climb from the bottom left while the axes stay where they are.
Why SVG here
anim_save() picks the format from the extension. This page uses .svg, which writes every frame as vector markup in a single file, stepped by CSS: crisp at any zoom, text that stays text, and a first frame held still for readers whose system asks for reduced motion.
An animated SVG grows with marks times frames, and 142 bubbles over 120 frames is about 3 MB on disk. But SVG is text, and web servers compress it: this file travels as under 200 KB, where the same animation as an animated PNG (.png) is about 5.5 MB and does not compress further. A raster format wins when a scene is truly dense (thousands of marks a frame, or a datashaded field); for a few hundred moving shapes, vector is the better trade. An animation with no pixels in it covers the format in more detail.