#Aishwarya Sivasubramanian, Luke Kaplan, Randa Ampah 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.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.0
## ── 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
library(dplyr)
gapminder_summary <- gapminder %>%
  group_by(continent) %>%
  summarize(
    average_gdpPercap = mean(gdpPercap, na.rm = TRUE),
    max_gdpPercap = max(gdpPercap, na.rm = TRUE)
  )
gapminder_summary
## # A tibble: 5 × 3
##   continent average_gdpPercap max_gdpPercap
##   <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"))

#Uncomment and modify the line below this one to create your plot!
static_plot <- 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

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

#Uncomment the line below and modify as needed!

animated_plot <- plot_ly(gapminder, x = ~gdpPercap, y = ~lifeExp, color = ~continent,
        size = ~pop, text = ~country, frame = ~year,
        hoverinfo = "text", type = 'scatter', mode = "markers") %>%
  layout(title = "Life Expectancy vs. GDP per Capita by Year",
         xaxis = list(title = "GDP per Capita"),
         yaxis = list(title = "Life Expectancy"))
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.

#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

Specifying frames to 100 makes it faster because before, our frame count was much lower than that. Redraw option makes the graph again every single frame, similar to a stop motion animation rather than the datapoints simply just moving along the graph, this is why it takes a lot long. A linear easing means that the animation has a controlled speed from beginning to end because it is a controlled speed. Autoplay = True means that the animation would start playing automatically without the user having to include any type of input.

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 = -.01,  
      y = -0.3,  
      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 title, axis labels, and other important annotations such as hover text and buttons earlier in the assignment. This is what we have learned makes a good plot, and so we’ve always included it. Our plot from Q5 has all of these elements.

Q7

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

https://rpubs.com/Aishwarya2904/1167532