Update Jul2018: gganimate()
has undergone a rewrite and is now much improved. The previous API is only available through the archived version and should not be expected to work with the new version.
With the new version:
library(gapminder)
library(gganimate)
## standard ggplot2
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
# Here comes the gganimate specific bits
labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear')
Producing the much smoother graphic
Original Answer:
I found the gganimate package to do quite well at this.
library(gapminder)
library(ggplot2)
theme_set(theme_bw())
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year)) +
geom_point() +
scale_x_log10()
library(gganimate)
gganimate(p)
gganimate(p, "output.gif")
Update Dec2016: gganimate()
now replaces gg_animate()
and adds cowplot
as a dependency (should auto-install once Issue #32 is resolved).