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.)
To learn more about streamgraphs in R, check out the blog post linked below.
Thanks for giving a shoutout to the package! The best way to embed widgets into blog post are to use iframes. Since I'm not sure a typepad comment will be able to handle the tags, I put a small gist together that shows how I embedded the widget into the Intro post you linked to:
https://gist.github.com/hrbrmstr/06977865d6a25213d122
Sadly, this does require the ability to host said iframe html code (in theory) on the same server. It might work from another location but some browser security settings will cause the content not to display.
You can also save the widget out in "partial" form (i.e. not full HTML wrappers) and embed the raw HTML, but site aggregators (i.e. r-bloggers) wince at the size of the posts then (default is to embed all the data and javascript).
The final option is to save the widget out and not save the data with it (nor embed the javascript) then reference that all externally in via <script> & <link> tags, but that's super-tedious.
Posted by: Bob Rudis | July 31, 2015 at 11:51
Thanks Bob, that did the trick! Updated the post above with an interactive version of the graphic.
Posted by: David Smith | July 31, 2015 at 11:58
Nice topic,
I was thinking how to visualise one problem and just found the solution.
I assume that I can use this package together with shine without any problems.
Posted by: Justas | August 03, 2015 at 02:11
I ment shiny by rstudio.
Posted by: Justas | August 03, 2015 at 02:12
You should be able to use Shiny with streamgraphs w/o issue. There are a couple issues relating to shiny but if you have any def let me know.
Posted by: Hrbrmstr | August 03, 2015 at 18:54