Plotting the baseline
You can use different data sets for different layers in plotly
, just like you can in ggplot2
. This is a useful strategy when you want to display a baseline time point (or group) in an animation. In this exercise, your task is to create an animated scatterplot of housing price index against real GDP, keeping the 1997 data points in the background.
Cet exercice fait partie du cours
Intermediate Interactive Data Visualization with plotly in R
Instructions
- Add static points representing the data from 1997 (found in
us1997
) as the first layer. Set thecolor
of these points to"gray60"
and theopacity
to0.5
. - Add a second trace to create an animated scatterplot over time (
year
) using the entireus_economy
data frame. Remember that each point represents astate
in any givenyear
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# extract the 1997 data
us1997 <- us_economy %>%
filter(year == 1997)
# create an animated scatterplot with baseline from 1997
us_economy %>%
plot_ly(x = ~gdp, y = ~house_price) %>%
add_markers(data = ___, marker = list(color = ___, opacity = ___)) %>%
add_markers(frame = ___, ids = ___, data = us_economy, showlegend = FALSE, alpha = 0.5) %>%
layout(xaxis = list(type = "log"))