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.
data <- gapminder
data %>%
group_by(continent) %>%
summarise(max = max(gdpPercap, na.rm=TRUE), avg=mean(gdpPercap, na.rm=TRUE))
## # A tibble: 5 × 3
## continent max avg
## <fct> <dbl> <dbl>
## 1 Africa 21951. 2194.
## 2 Americas 42952. 7136.
## 3 Asia 113523. 7902.
## 4 Europe 49357. 14469.
## 5 Oceania 34435. 18622.
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(data = gapminder,
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
marker = list(sizemode = "diameter", opacity = 0.5),
color = ~continent,
mode = "markers",
type = 'scatter')
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.
# Code goes here
#Uncomment the line below and modify as needed!
animated_plot <- plot_ly(data = gapminder,
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
marker = list(sizemode = "diameter", opacity = 0.5),
color = ~continent,
mode = "markers",
type = 'scatter',
frame = ~year)
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.
# Uncomment the lines below to see the updated animation.
animated_chart <- animated_plot %>%
animation_opts(#frame = 100,
redraw = TRUE,
easing = "cubic",
#autoplay = TRUE
)
animated_chart
frame: controls how fast the years switch (I would assume time on each frame in milliseconds) easing: changes how the points transition between years, not very easy to see when set to linear, but you can see it better when it is set to cubic autoplay: automatically starts the animation redraw: this does not seem to do anything on this plot, but I would assume when the data doesnt have as smooth of transitions it just completely redraws the points instead of trying to transition them
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 = 20,
y = 100,
buttons = list(
list(
label = "Pause",
method = "animate",
args = list(NULL, list(frame = list(duration = 0,
redraw = TRUE),
mode = "immediate"))
)
)
)
)
)
interactive_chart
Add a title, axis labels, any other important annotations to the plot. This could include adding hover text or other elements.
plot_ly(data = gapminder,
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
marker = list(sizemode = "diameter", opacity = 0.5),
color = ~continent,
text = ~paste("Country: ", country,
"<br>GDP: ", gdpPercap,
"<br>Population: ", pop),
frame = ~year,
mode = "markers",
type = 'scatter') %>%
layout(title = "Life Expectancy vs GDP",
xaxis = list(title = "GDP Per Cap", type = "log"),
yaxis = list(title = "Life Expectancy")) %>%
animation_opts(
easing = "cubic"
)
Practice publishing your final plot to Rpubs (or another location of your choice) and include the link in your submission.