knitr::opts_chunk$set(echo=TRUE, warning=FALSE)

Link: https://rpubs.com/gvt8kv/ICA11

Caitlin Grant and Catherine Nguyen

install.packages(“gapminder”)

library(gapminder)    

Question 1

Variables in dataset: country, continent, year, lifeExp, pop, gdpPercap. A summary of the average and maximum GDP by continent was calculated.

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
gdp_summary <- gapminder %>%
    group_by(continent) %>%
    summarize(avg_gdp = mean(gdpPercap * pop), max_gdp = max(gdpPercap * pop))
print(gdp_summary)
## # 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

Question 2: bubble chart

install.packages(“plotly”) # Run this once to install Plotly

We created a bubble plot with GDP on the x axis, life expectancy and population as the size of the bubbles. The colors of the bubbles are based on the region of that nation.

library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
plot_ly(gapminder, 
        x = ~gdpPercap, 
        y = ~lifeExp, 
        size = ~pop, 
        color = ~continent, 
        type = 'scatter', 
        mode = 'markers',
        marker = list(sizemode = "diameter"))

Question 3

We created a new plotly object that is similar to the previous one, but includes animation. We added frame with year as the variable.

animated_plot <- plot_ly(gapminder, 
                         x = ~gdpPercap, 
                         y = ~lifeExp, 
                         size = ~pop, 
                         color = ~continent, 
                         frame = ~year, 
                         type = 'scatter', 
                         mode = 'markers',
                         marker = list(sizemode = "diameter"))
animated_plot

Question 4

We few options to alter your animation lightly including frame, redraw and easing. We took out autoplot = true as this resulted in an error.

# Frame: This is the time that each framelasts (100 milliseconds)
# Redraw: This is redrawing a new plot each time a new frame is shown 
# Easing: This is a trnasiton between frame at a speed constant 
animated_chart <- animated_plot %>% 
    animation_opts(frame = 100,       
                   redraw = TRUE,     
                   easing = "linear") 
animated_chart

Question 5

We moved the button to the bottom right corner as this is a more visible place to put it.

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

interactive_chart

Question 6

We already added a title and axes label in the previous questions above.

final_chart <- animated_plot %>%
    layout(
        title = "Gapminder Data Over Time",
        xaxis = list(title = "GDP per Capita"),
        yaxis = list(title = "Life Expectancy"),
        hovermode = "closest",
        updatemenus = list(
            list(
                type = "buttons",
                showactive = FALSE,
                x = 1,             
                y = 0,             
                xanchor = "right", 
                yanchor = "bottom", 
                buttons = list(
                    list(
                        label = "Pause",
                        method = "animate",
                        args = list(NULL, list(frame = list(duration = 0, redraw = TRUE),
                                               mode = "immediate"))
                    )
                )
            )
        )
    ) %>%
    plotly::style(
        hoverinfo = "text",
        text = ~paste("Country:", country, "<br>GDP per Capita:", gdpPercap,
                      "<br>Life Expectancy:", lifeExp, "<br>Population:", pop)
    )

final_chart