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)
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
data("gapminder")
gapminder %>%
group_by(continent) %>%
summarize(Average_GDP = mean(gdpPercap),
Maximum_GDP = max(gdpPercap))
## # A tibble: 5 × 3
## continent Average_GDP Maximum_GDP
## <fct> <dbl> <dbl>
## 1 Africa 2194. 21951.
## 2 Americas 7136. 42952.
## 3 Asia 7902. 113523.
## 4 Europe 14469. 49357.
## 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 <- plot_ly(data = gapminder,
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
color = ~continent,
text = ~country,
mode = "markers") %>%
layout(title = "Gapminder Dataset: GDP vs Life Expectancy (Bubble Chart)",
xaxis = list(title = "GDP per Capita"),
yaxis = list(title = "Life Expectancy"),
showlegend = TRUE)
static_plot
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#scatter
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 variable that should determine the
frame.
animated_plot <- plot_ly(data = gapminder,
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
color = ~continent,
text = ~country,
frame = ~year,
mode = "markers") %>%
layout(title = "Gapminder Dataset: GDP vs Life Expectancy (Animated Bubble Chart)",
xaxis = list(title = "GDP per Capita"),
yaxis = list(title = "Life Expectancy"),
showlegend = TRUE)
animated_plot
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#scatter
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")
#autoplay = TRUE
animated_chart
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#scatter
frame: Specifies the amount of time between frames (in milliseconds. In this case, frame = 100 means each frame will last for 100 milliseconds.
redraw: Triggers a redraw of the plot at completion of the transition. If redraw = TRUE, the plot will be redrawn for each frame.
easing: Specifies the easing function to be used for the animation transition. Easing functions control the speed of animation over time. In this case, “linear” is specified, which means the animation will progress at a constant speed throughout.
autoplay: Specifies whether the animation should start automatically when the plot is displayed. When autoplay = TRUE, the animation will start playing without requiring any user interaction.
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,
y = -0.08,
buttons = list(
list(
label = "Pause",
method = "animate",
args = list(NULL, list(frame = list(duration = 0,
redraw = TRUE),
mode = "immediate"))
)
)
)
)
)
interactive_chart
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#scatter
We moved the button slightly downwards so it’s closer to the play button and is not covering any axis.
Add a title, axis labels, any other important annotations to the plot. This could include adding hover text or other elements.
final_chart <- interactive_chart %>%
layout(
title = "Interactiive Comparison Bubble Chart",
xaxis = list(title = "GDP PER Capita"),
yaxis = list(title = "life Expectancy"),
showlegend = TRUE,
legend = list(title = list(text= "Continent"))
)
final_chart
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#scatter
Practice publishing your final plot to Rpubs (or another location of your choice) and include the link in your submission.