Note the use of the p_load() function from the pacman package!
library(gapminder)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ lubridate 1.9.3 ✔ 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(gganimate)
library(gifski)
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.
gap <- gapminder
sum_gap<- gap %>%
group_by(continent) %>%
summarise(
average_GDP = mean(gdpPercap),
max_GDP = max(gdpPercap)
)
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"))
static_plot <- plot_ly(gap, 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"))
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 <- plot_ly(gap, x = ~gdpPercap, y = ~lifeExp, color = ~continent,
size = ~pop, text = ~country,
hoverinfo = "text", type = 'scatter', mode = "markers",
frame = ~year) %>%
layout(title = "Life Expectancy vs. GDP per Capita",
xaxis = list(title = "GDP per Capita"),
yaxis = list(title = "Life Expectancy"))
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,
redraw = TRUE,
easing = "linear")
(frame = 100) argument changed the time in between each year. It sped up the animation. The redraw argument it replots the data for every frame, but it is not noticeable. (easing = linear) means that it will transition smoothly in a constant speed.
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 = 1,
y = 0,
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.
animated_chart %>% layout(
updatemenus = list(
list(
type = "buttons",
showactive = FALSE,
x = 1,
y = 0,
buttons = list(
list(
label = "Pause",
method = "animate",
args = list(NULL, list(frame = list(duration = 0,
redraw = TRUE),
mode = "immediate"))
)
)
)
)
)
We already added a title, axis labels, and other annotations so we are just showing the final plot again.
Practice publishing your final plot to Rpubs (or another location of your choice) and include the link in your submission.