Note the use of the p_load() function from the pacman package!
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(gapminder)
summary(gapminder)
FALSE country continent year lifeExp
FALSE Afghanistan: 12 Africa :624 Min. :1952 Min. :23.60
FALSE Albania : 12 Americas:300 1st Qu.:1966 1st Qu.:48.20
FALSE Algeria : 12 Asia :396 Median :1980 Median :60.71
FALSE Angola : 12 Europe :360 Mean :1980 Mean :59.47
FALSE Argentina : 12 Oceania : 24 3rd Qu.:1993 3rd Qu.:70.85
FALSE Australia : 12 Max. :2007 Max. :82.60
FALSE (Other) :1632
FALSE pop gdpPercap
FALSE Min. :6.001e+04 Min. : 241.2
FALSE 1st Qu.:2.794e+06 1st Qu.: 1202.1
FALSE Median :7.024e+06 Median : 3531.8
FALSE Mean :2.960e+07 Mean : 7215.3
FALSE 3rd Qu.:1.959e+07 3rd Qu.: 9325.5
FALSE Max. :1.319e+09 Max. :113523.1
FALSE
library(dplyr)
gapminder %>%
group_by(continent) %>%
summarize(avg_gdp = mean(gdpPercap, na.rm = TRUE),
max_gdp = max(gdpPercap, na.rm = TRUE))
FALSE # A tibble: 5 × 3
FALSE continent avg_gdp max_gdp
FALSE <fct> <dbl> <dbl>
FALSE 1 Africa 2194. 21951.
FALSE 2 Americas 7136. 42952.
FALSE 3 Asia 7902. 113523.
FALSE 4 Europe 14469. 49357.
FALSE 5 Oceania 18622. 34435.
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"))
library(plotly)
static_plot <- gapminder %>%
plot_ly(x = ~gdpPercap, y = ~lifeExp, size = ~pop, color = ~continent, text = ~country,
sizes = c(5, 100), marker = list(sizemode = "diameter")) %>%
layout(xaxis = list(title = "GDP"),
yaxis = list(title = "Life Expectancy"),
title = "Gapminder Data: GDP vs Life Expectancy by Country",
showlegend = FALSE)
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?
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.
animated_plot <- gapminder %>%
plot_ly(x = ~gdpPercap, y = ~lifeExp, size = ~pop, color = ~continent,
frame = ~year, text = ~country,
sizes = c(5, 100), marker = list(sizemode = "diameter")) %>%
layout(xaxis = list(title = "GDP"),
yaxis = list(title = "Life Expectancy"),
title = "GDP vs Life Expectancy by Country",
showlegend = TRUE)
animated_plot
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, # Sets the duration of each frame to 100 milliseconds
redraw = TRUE, # If true redraws the frame
easing = "linear") # Easing function for the animation
#autoplay = TRUE # starts animation automatically if true
animated_chart
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.2,
y = -.1,
buttons = list(
list(
label = "Pause",
method = "animate",
args = list(NULL, list(frame = list(duration = 0,
redraw = TRUE),
mode = "immediate"))
)
)
)
)
)
interactive_chart
We added a title, axis titles, and a legend (in the third question)
Practice publishing your final plot to Rpubs (or another location of your choice) and include the link in your submission.