Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

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.)