To pass specifications, you need to complete all questions, especially the final couple questions. You should have a functional animation with a working “play” button and some customized options. The animated line document on Canvas may be useful as another reference for animations using Plotly in R.

Note the use of the p_load() function from the pacman package!

knitr::opts_chunk$set(echo = TRUE)
pacman::p_load(plotly, gapminder)

Q1

Load the gapminder dataset into your R environment and take a look at it. Note the location of different variables and include a summary of the average and maximum GDP by continent.

gapminder_gdp <- gapminder %>%
  mutate(gdp_total = gdpPercap * pop) %>%
  group_by(continent) %>%
  summarise(
    avg_gdp = mean(gdp_total, na.rm = TRUE),
    max_gdp = max(gdp_total, na.rm = TRUE)
  )
head(gapminder_gdp)
## # A tibble: 5 × 3
##   continent       avg_gdp max_gdp
##   <fct>             <dbl>   <dbl>
## 1 Africa     20904782844. 4.48e11
## 2 Americas  379262350210. 1.29e13
## 3 Asia      227233738153. 6.54e12
## 4 Europe    269442085301. 2.65e12
## 5 Oceania   188187105354. 7.04e11

Q2

Using plotly, create a static bubble chart of the gapminder data. Map GDP to the x axis, life expectancy to the Y axis, and population to the size of the bubbles. Each bubble’s color should be based on the region of that nation. Hint: If size is not working properly for you, you may want to use: marker = list(sizemode = "diameter"))

#Uncomment and modify the line below this one to create your plot!

library(plotly)
library(gapminder)

plot_ly(gapminder, x = ~gdpPercap, y = ~lifeExp, color = ~continent,
        size = ~pop, text = ~country,
        hoverinfo = "text", type = 'scatter', mode = "markers") %>%
  layout(title = "Life Expectancy vs. GDP per Capita",
         xaxis = list(title = "GDP per Capita"),
         yaxis = list(title = "Life Expectancy"))
#static_plot <- plot_ly(...)

Note here that the plot looks pretty messy because all the years are present on the plot. It would be nice to see the dots by country one year at a time, right?

Q3

Create a new plotly object that is similar to the previous one, but includes animation. This is as easy as adding frame to your plot_ly command and specifying the varable that should determine the frame.

library(plotly)
library(gapminder)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
df <- gapminder
animated_plot <- df %>%
  plot_ly(
    x = ~gdpPercap, y = ~lifeExp, color = ~continent,
    size = ~pop, frame = ~year, text = ~country,
    type = 'scatter', mode = 'markers',
    marker = list(sizemode = 'diameter', sizeref = 2)
  ) %>%
  layout(xaxis = list(type = "log", title = "GDP per Capita"),
         yaxis = list(title = "Life Expectancy")) %>%
  animation_opts(frame = 100, redraw = TRUE, easing = "linear")

animated_plot
#Uncomment the line below and modify as needed!
#animated_plot <- plot_ly(...)

Q4

Try adding a few options to alter your animation lightly. As long as you’ve created your plot correctly, all you need to do here is uncomment the code, run the chunk, and explain what each part did.

The ‘frame’ option sets the duration to 100 frames per second. The ‘redraw’ option re-renders each frame to make the visulization more dynamic. It’s important for when an animated plot needs to be updated frame-by-frame. The ‘easing’ option means that the animation proceeds at a constant speed without acceleration or deceleration.

#Uncomment the lines below to see the updated animation. 
animated_chart <- animated_plot %>% 
  animation_opts(frame = 100,  #What did this do?
                redraw = TRUE, # What did this do?
                 easing = "linear") #What did this do?
                 #autoplay = TRUE) #What did this do)
animated_chart

Q5

Now, let’s add a button to pause the animation. You should spend a bit of time looking at this code and understanding what it does, then try to move the button to a place that makes more sense.

 interactive_chart <- animated_chart %>% layout(
   updatemenus = list(
     list(
       type = "buttons",
       showactive = FALSE,
       x = 0,  
       y = -0.5,  
       buttons = list(
         
         list(
           label = "Pause",
           method = "animate",
           args = list(NULL, list(frame = list(duration = 0, 
                                               redraw = TRUE), 
                                  mode = "immediate"))
         )
       )
     )
   )
 )
interactive_chart

Q6

Add a title, axis labels, any other important annotations to the plot. This could include adding hover text or other elements.

library(plotly)
library(gapminder)
library(dplyr)

df <- gapminder
animated_plot1 <- df %>%
  plot_ly(
    x = ~gdpPercap, y = ~lifeExp, color = ~continent,
    size = ~pop, frame = ~year, text = ~country,
    type = 'scatter', mode = 'markers',
    marker = list(sizemode = 'diameter', sizeref = 2)
  ) %>%
  layout(xaxis = list(type = "log", title = "GDP per Capita (Dollars)"),
         yaxis = list(title = "Life Expectancy (Years)"),
         title="GDP per Capita vs. Life Expectancy (1952-2007), by Continent") %>%
  animation_opts(frame = 100, redraw = TRUE, easing = "linear")



animated_chart1 <- animated_plot1 %>% 
  animation_opts(frame = 100,  #What did this do?
                redraw = TRUE, # What did this do?
                 easing = "linear") #What did this do?
                 #autoplay = TRUE) #What did this do)

interactive_chart1 <- animated_chart1 %>% layout(
   updatemenus = list(
     list(
       type = "buttons",
       showactive = FALSE,
       x = 0,  
       y = -0.5,  
       buttons = list(
         
         list(
           label = "Pause",
           method = "animate",
           args = list(NULL, list(frame = list(duration = 0, 
                                               redraw = TRUE), 
                                  mode = "immediate"))
         )
       )
     )
   )
 )
interactive_chart1

Q7

Practice publishing your final plot to Rpubs (or another location of your choice) and include the link in your submission.