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

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.

data(gapminder)
frame1 <- gapminder %>%
  group_by(continent) %>%
  summarise(
    Average_GDP = mean(gdpPercap),
    Maximum_GDP = max(gdpPercap)
  )

print(frame1)
## # A tibble: 5 × 3
##   continent Average_GDP Maximum_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"))

gapminder %>%
  plot_ly(x = ~gdpPercap, y = ~lifeExp, 
          marker = list(sizemode = "diameter", sizeref = 1),
          color = ~continent, colors = "Set1") %>%
  layout(xaxis = list(title = "gdpPercap"),
         yaxis = list(title = "lifeExp"),
         title = "Gapminder Bubble Chart")
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode

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.

gapminder %>%
  plot_ly(x = ~gdpPercap, y = ~lifeExp, frame = ~year,
          marker = list(sizemode = "diameter", sizeref = 1),
          color = ~continent, colors = "Set1") %>%
  layout(xaxis = list(title = "gdpPercap"),
         yaxis = list(title = "lifeExp"),
         title = "Gapminder Bubble Chart")
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode

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.

library(plotly)

gapminder %>% 
  plot_ly(x = ~gdpPercap, y = ~lifeExp, frame = ~year,
          marker = list(sizemode = "diameter", sizeref = 1),
          color = ~continent, colors = "Set1") %>%
  layout(xaxis = list(title = "gdpPercap"),
         yaxis = list(title = "lifeExp"),
         title = "Gapminder Bubble Chart") -> animated_chart

animated_chart <- animated_chart %>% 
  animation_opts(frame = 100, redraw = TRUE, easing = "linear")

animated_chart <- animated_chart %>% 
  animation_slider(currentvalue = list(prefix = "Year:"))
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
animated_chart

frame=100, this function displays the duration of the animation at 100 milliseconds. redraw=TRUE, this function redraws the plot for each frame of the animation easing=“linear” This function will change the style of the animation autoplay=TRUE it enables the animation on its own without having to press play

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.

library(plotly)

gapminder %>% 
  plot_ly(x = ~gdpPercap, y = ~lifeExp, frame = ~year,
          marker = list(sizemode = "diameter", sizeref = 1),
          color = ~continent, colors = "Set1") %>%
  layout(xaxis = list(title = "gdpPercap"),
         yaxis = list(title = "lifeExp"),
         title = "Gapminder Bubble Chart") -> animated_chart

animated_chart <- animated_chart %>% 
  animation_opts(frame = 100, redraw = TRUE, easing = "linear")

animated_chart <- animated_chart %>% 
  animation_slider(currentvalue = list(prefix = "Year:"))
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
animated_chart <- animated_chart %>% 
  layout(
    updatemenus = list(
      list(
        type = "buttons",
        showactive = FALSE,
        x = 1.05, 
        y = 0.95,   
        buttons = list(
          list(
            label = "Pause",
            method = "animate",
            args = list(NULL, list(frame = list(duration = 0, redraw = TRUE), mode = "immediate"))
          )
        )
      )
    )
  )

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

gapminder %>% 
  plot_ly(x = ~gdpPercap, y = ~lifeExp, frame = ~year,
          marker = list(sizemode = "diameter", sizeref = 1),
          color = ~continent, colors = "Set1",
          text = ~paste("Country: ", country, "<br>Year: ", year,
                        "<br>GDP per Capita: $", sprintf("%.2f", gdpPercap),
                        "<br>Life Expectancy: ", sprintf("%.2f", lifeExp))) %>%
  layout(
    xaxis = list(title = "GDP per Capita"),
    yaxis = list(title = "Life Expectancy"),
    title = "Gapminder Bubble Chart",
    showlegend = FALSE,  
    updatemenus = list(
      list(
        type = "buttons",
        showactive = FALSE,
        x = 1.05, 
        y = 0.95,  
        buttons = list(
          list(
            label = "Pause",
            method = "animate",
            args = list(NULL, list(frame = list(duration = 0, redraw = TRUE), mode = "immediate"))
          )
        )
      )
    )
  ) -> animated_chart

animated_chart <- animated_chart %>% 
  animation_opts(frame = 100, redraw = TRUE, easing = "linear")

animated_chart <- animated_chart %>% 
  animation_slider(currentvalue = list(prefix = "Year:"))
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
animated_chart

Q7

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