The gganimate package lets you add animation to your ggplots in R. In this video I demonstrate how to recreate animated bubble plots like the http://gapminder.org site and how to save them as gif files that you can use in publications, websites, presentations, and on social media (they are particularly popular on LinkedIn). The code used in the video can be found below.
Subscribe below to get updates on my latest videos, courses, and other useful information.
library(ggplot2)
library(gganimate)
library(gapminder)
# create animated ggplot
gapplot<-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() +
facet_wrap(~continent) +
# Here comes the gganimate specific bits
labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear')
# view the animated plot
gapplot
# save as a gif
anim_save("gapmind.gif",gapplot)