Streamgraph with dropdown button



The streamgraph package allows to add a dropdown button that controls which group to highlight. See how to use this option with explanation and reproducible code.

Streamgraph section Data to Viz

It is pretty straightforward to add a dropdown button thanks to the sg_legend() function. Unlike other streamgraph() options, sg_legend() is used after the pipe command (%>%).

Note: you can save the chart as a html file using the saveWidget() function of the htmlwidgets package, as suggested in the commented code below.

# Library
library(streamgraph)
 
# Create data:
data <- data.frame(
  year=rep(seq(1990,2016) , each=10),
  name=rep(letters[1:10] , 27),
  value=sample( seq(0,1,0.0001) , 270)
)
 
# Stream graph with a legend
pp <- streamgraph(data, key="name", value="value", date="year", height="300px", width="1000px") %>%
  sg_legend(show=TRUE, label="names: ")
  

# save the widget
# library(htmlwidgets)
# saveWidget(pp, file=paste0( getwd(), "/HtmlWidget/streamgraphDropdown.html"))

Related chart types


Line plot
Area
Stacked area
Streamchart
Time Series



❤️ 10 best R tricks ❤️

👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥