Adding a slider for time
In the previous lesson, you created an animation displaying the number of launches by each state over the years. A slider bar is an alternative to that animation that gives you full control over what portion of the story to investigate. For example, you can view it from beginning to end or focus only on the 1960s. In this exercise, your task is to add a slider bar below the line chart of state-based launches.
plotly
, crosstalk
, dplyr
, and the state_launches
data set have been loaded for you.
This exercise is part of the course
Intermediate Interactive Data Visualization with plotly in R
Exercise instructions
- Calculate the number of launches by
state_code
andlaunch_year
, and convert this to aSharedData
object. Store the result inshared_launches
. - Using
shared_launches
, create a line chart displaying the the number of launches (n
) by each state over time. Store this chart aslaunch_ts
. Use color to represent thestate_code
. - Position a slider below the chart to filter the years displayed. Label this slider
"Year"
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a SharedData object containing the number of launches by year and state
shared_launches <- state_launches %>%
count(___, ___) %>%
___()
# Create a line chart displaying the launches by state
launch_ts <- ___ %>%
___(x = ___, y = ___, color = ___) %>%
___()
# Add a slider below the chart to filter the years displayed
___(list(___,
___(id = "time", label = ___,
sharedData = ___, column = ___)
))