Introduction

This code through project explores the gganimate() package of ggplot() to add some transition effects over time using the gapminder dataset. I will compare life expectancy to GDP per capita across the various countries from 1952 to 2007.


Content Overview

Specifically, I’ll explain and demonstrate how to load the gganimate() package, add transition effect, add a time frame, show trails of how each point moves across time, edit the graph size, and save the graph as a gif.


Why You Should Care

This topic is valuable because gganimate() gives a 3D view of graphs compared to the traditional static graphs, especially when observed across time. It adds beautiful aesthetics to plots and makes presentations fun. Finally, because we can save the final plot as a gif, sharing on various platforms is easy.


Learning Objectives

Specifically, you’ll learn how to animate plots in R using gganimate() and save the graph as a gif file.



Body Title

Here, we’ll show how life expectancy and GDP per capita vary across time for each country using the gapminder dataset.


Further Exposition

This is based on the work of Ola Rosling, Anna Rosling Rönnlund, and Hans Rosling. Gapminder was created in February 2005 to combine data from multiple sources into unique, coherent time-series. The dataset contains variables such as life expectancy, population, GDP per capita, years, countries, and continents.


Basic Example

This basic example shows how to plot the life expectancy vs. GDP per capita using ggplot() and add a simple transition effect.

plot1 = gapminder %>% 
  ggplot(aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) + 
  geom_point(alpha = 0.7, stroke = 0) + 
  theme_fivethirtyeight() + 
  scale_size(range = c(2, 12), guide = "none") + 
  scale_x_log10() + 
  labs(title = "Life Expectancy vs GDP per Capita by Country",
        x = "GDP Per Capita", 
        y = "Life Expectancy", 
        color = "Continent",
        caption = "Source: Hans Rosling's gapminder data on health and wealth") + 
  theme(axis.title = element_text(), 
        text = element_text(family = "serif"), 
        legend.text = element_text(size = 10)) + 
  scale_color_brewer(palette = "Set2")

plot1.animate = plot1 + 
  transition_time(year) # adding the transition effect

plot1.animate


Advanced Examples

More specifically, this code can be used for adding a time frame to the plot to display the changes in time as the points change.

# Add year frame to show what year it is. 

plot1.animate = plot1 + 
  transition_time(year) + 
  labs(subtitle = "Year: {frame_time}")


plot1.animate


Moreover, this additional code can also be used for adding shadows to each point that serve as trails to demonstrate the paths taken by the points as they change over time.

# Add shadow wake trail to show how the point are moving over time 

plot1.animate = plot1 + 
  transition_time(year) + 
  labs(subtitle = "Year: {frame_time}") + 
  shadow_wake(wake_length = 0.1)

plot1.animate


Most notably, the gganimate() package allows us to save the plots in GIF format to share on various platforms, keeping the animation effect intact.

# Saving and sharing as gif 

plot1.animate = plot1 + 
  transition_time(year) + 
  labs(subtitle = "Year: {frame_time}") + 
  shadow_wake(wake_length = 0.1) 

animate(plot1.animate, height = 500, width = 800, fps = 30, duration = 10, 
        end_pause = 60, res = 100) 

anim_save("gapminder plot.gif") 

plot1.animate



Further Resources

Learn more about [package, technique, dataset] with the following:




Works Cited

This code through references and cites the following sources: