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(warning = FALSE, message = FALSE, echo = TRUE)
pacman::p_load(plotly, gapminder)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

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
## # A tibble: 1,704 × 6
##    country     continent  year lifeExp      pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Afghanistan Asia       1952    28.8  8425333      779.
##  2 Afghanistan Asia       1957    30.3  9240934      821.
##  3 Afghanistan Asia       1962    32.0 10267083      853.
##  4 Afghanistan Asia       1967    34.0 11537966      836.
##  5 Afghanistan Asia       1972    36.1 13079460      740.
##  6 Afghanistan Asia       1977    38.4 14880372      786.
##  7 Afghanistan Asia       1982    39.9 12881816      978.
##  8 Afghanistan Asia       1987    40.8 13867957      852.
##  9 Afghanistan Asia       1992    41.7 16317921      649.
## 10 Afghanistan Asia       1997    41.8 22227415      635.
## # ℹ 1,694 more rows
summary.gapminder <- gapminder %>% 
  group_by(continent) %>% 
  summarize(avg.gdp = mean(gdpPercap),
          max.gdp = max(gdpPercap)
          )
summary.gapminder
## # A tibble: 5 × 3
##   continent avg.gdp max.gdp
##   <fct>       <dbl>   <dbl>
## 1 Africa      2194.  21951.
## 2 Americas    7136.  42952.
## 3 Asia        7902. 113523.
## 4 Europe     14469.  49357.
## 5 Oceania    18622.  34435.

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"))

static_plot <- gapminder %>% 
  plot_ly(x = ~gdpPercap, y = ~lifeExp, size = ~pop, color = ~continent, marker = list(sizemode = 'diameter'))
    
static_plot

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.

# Code goes here
animated_plot <- gapminder %>% 
  plot_ly(x = ~gdpPercap, y = ~lifeExp, size = ~pop, color = ~continent, 
          marker = list(sizemode = 'diameter'), frame = ~year, text = ~country)
animated_plot

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.

animated_chart <- animated_plot %>% 
  animation_opts(frame = 100,  #This sets the amount of time between frames to be 100 milliseconds. The transition between years goes faster. 
                 redraw = TRUE, # This does not make a significant impact here. In other cases, however, redraw() redraws the whole plot between transitions. 
                 easing = "linear") # This makes the transition linear from one frame to the next. It's 
                 #autoplay = TRUE # This does not work on my computer. However, autoplay() automatically plays the animation when the plot is rendered.)
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(
   title = "GDP per Capita vs. Life Expectancy per Country by Year",
   updatemenus = list(
     list(
       type = "buttons",
       showactive = FALSE,
       x = 0,  
       y = -0.05,  # I moved the pause button closer to the play button to make it easier to quickly press pause after pressing play. 
       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. - We added a plot title and the country name to each point annotation.

Q7

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

https://rpubs.com/penny-on-sidewalk/1242944