# load libraries
library(tidyverse)

In this complement mini-assignment, you are going to use the following data set.

D1 <- read_csv("processed-20220221-owid-life-expectancy-vs-gdp-per-capita.csv")


  1. Basic Interactive Figure using plotly. Modify the R code below where you choose four countries other than what is written. The code below makes an interactive time-series plots for four countries showing the gdp_per_capita variable in time. Use the scale_color_colorblind() function to change the colors.
# load packages
library(ggthemes)
library(plotly)

##### start: Write you chosen countries here
chosen_countries <- c("Philippines","United States","Japan","Brazil")
##### end: Write you chosen countries here

# subset the data
D1_sub <- D1 %>%
  # Use the %in% command to filter-in only the chosen countries
  filter(country %in% chosen_countries) %>%
  drop_na()

# create a time-series plot
p1 <- ggplot(data = D1_sub, aes(x = year, 
                                y = gdp_per_capita,
                                color = country)) + 
  geom_point() + 
  geom_line() +
#### start: Write plot modifications here

#### end: Write plot modifications here
  labs(x = "year",
       y = "gdp per capita",
       title = "Yearly GDP per Capita")

# render the ggplot using plotly - interactive
ggplotly(p1)


Basic Animated Figure using gganimate.

# load gganimate package
# you need to install gganimate, gifski, transformr and png packages & restart R
library(gganimate)

# animate the time-series
p1_anim <- p1 + 
  geom_point(aes(group = seq_along(year))) + 
  transition_reveal(year)
animate(p1_anim,
        #fps = 20, duration = 15, # duration and frame rate
        #height = 4, width = 8, units = "in", res = 150 # size and resolution
)

# save the animation
anim_save("time_series_gganimate.gif")


  1. Multidimensional Interactive Figure using plotly. Filter the data set to only include year 2018, do not include the “Antarctica” continent, and use the gdp_per_capita and life_expectancy variables to create an interactive scatter plot, use the facet_wrap() to separate the figure by continent.

Multidimensional Animated Figure using gganimate. Create an animated scatter plot using the gdp_per_capita and life_expectancy variables while using the facet_wrap() to separate the figure by continent. Do not include the “Antarctica” continent. Note that you need to use the year variable to do the transitions. Save the resulting gif file.

Submit the resulting html and the gif files using the Google form.

References

Max Roser, E. O.-O., & Ritchie, H. (2013). Life expectancy. Our World in Data. https://ourworldindata.org/life-expectancy