Streamgraphs in R

July 31, 2015

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

It’s not easy to visualize a quantity that varies over time and which is composed of more than two subsegments. Take, for example, this stacked bar chart of religious affiliation of the Australian population, by time:


While it’s easy to see the how the share of Anglicans (at the bottom of the chart) has changed over time, it’s much more difficult to assess the change in the “No religion” category: the separated bars coupled with the (necessarily) uneven positioning makes it hard to judge changes from year to year.

There’s no easy solution, but one increasingly popular idea is to use streamgraphs, which connect categories together into continuous polygons, and can even be aligned to the middle so that no one category gets favoured status (like the Anglican category above). Here’s a pioneering example of streamgraphs from the New York Times in 2008:

NYT movies 2008

If you click the image above, you’ll see that one nice feature is that you can hover your mouse over a segment and see it highlighted, which makes it a little easier to observe changes over time within a segment.

You can easily make interactive streamgraphs like this in R, with the streamgraph package, available on GitHub. The streamgraph function makes use of on htmlwidgets, and has a ggplot2-style object interface which makes it easy to create and customize your chart. There’s a nice demo on RPubs, from which I took this example:

stocks %>% 
  mutate(date=as.Date(quarter, format="%m/%d/%y")) %>% 
  streamgraph(key="ticker", value="nominal", offset="expand") %>% 
  sg_fill_manual(stock_colors) %>% 
  sg_axis_x(tick_interval=10, tick_units="year") %>% 
  sg_legend(TRUE, "Ticker: ")

The resulting streamgraph is shown below. (Update: Thanks to Bob Rudis in the comments for the tip on embedding htmlwidgets into blog posts.)

To learn more about streamgraphs in R, check out the blog post linked below. Introducing the streamgraph htmlwidget R Package

To leave a comment for the author, please follow the link and comment on their blog: Revolutions. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)