Motivation

The Economist

Topic: Average annual hours worked by persons engaged

Year: 1988-2017

Animation

  library(gganimate)
  library(gifski)
  library(transformr)
  
  graph1 <- df_plot %>% 
    ggplot(aes(year, avh, group = country, color = country)) +
    geom_line(size = 1.25, show.legend = TRUE) +
    theme_hc() +
    scale_color_manual(values = my_colors) +
    scale_x_continuous(limits = c(1986, 2017), breaks = seq(1986, 2017, 1), 
                       labels = label_y, expand = c(0, 0)) +
    scale_y_continuous(breaks = seq(1450, 3000, 250), limits = c(1450, 3000)) +
    theme(panel.grid.minor = element_blank()) +
    theme(panel.grid.major = element_line(color = "grey40", size = 0.2))+
    theme(plot.margin = unit(c(0.7, 1, 1, 1), "cm")) +
    labs(title = "Average annual hours worked by persons engaged", 
         subtitle = "Average annual hours worked is defined as the total number of hours actually worked per year\ndivided by the average number of people in employment per year.", 
         caption = "Source: The Penn World Table (PWT)") +
    theme(plot.title = element_text(size = 20, color = "grey10", face = "bold"))+
    theme(plot.subtitle = element_text(size = 12, color = "grey20", hjust = 0, margin = margin(b = 6)))+
    theme(plot.caption = element_text(size = 8, color = "grey30", hjust = 1, margin = margin(t = 6))) +
    theme(axis.title.x = element_blank()) +
    theme(axis.title.y = element_blank()) +
    theme(axis.text.x = element_text(size = 12, color = "grey30")) +
    theme(axis.text.y = element_text(size = 12, color = "grey30")) +
    theme(legend.title = element_blank())+
    theme(legend.position = "right")+
    theme(legend.text =  element_text(size = 10, color = "grey20"))
  
  graph1

Option 1

  graph.animation <-  graph1 +
    geom_point() +
    transition_reveal(year)
  
  animate(graph.animation,height = 640, width = 985, fps = 25, duration = 10, 
          end_pause = 60, res = 100)
  anim_save("Results/Animation.gif")

Option 2

  graph.animation.o <-  graph1 +
    transition_reveal(year) +
    view_follow(fixed_y = TRUE)
  
  animate(graph.animation.o,height = 640, width = 985, fps = 25, duration = 10, 
          end_pause = 60, res = 100)
  anim_save("Results/Animation1.gif")