Waterfall plots in R

[This article was first published on James Keirstead » Rstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Last week I posted about an orphaned paper of mine, a piece of research that hasn’t yet found a home and which I’m unlikely to find further time to work on. Abandoning something that you’ve worked hard on is disappointing and so it made sense to release it for those who were interested. At least that way, I can salvage some value from the whole exercise. There’s another element of the paper that might be useful too, at least for the R community: waterfall charts. Here’s the example from the paper: Summary of changes to global building energy demand in 2050 by intervention and sector. And here’s another example from the Carbon Trust, about carbon flows and the UK economy. Carbon emissions embodied in UK trade.  Source: Carbon Trust As you can see, these are effectively bar charts but designed to be read left-to-right so that you can see how a series of intermediate steps leads to a final conclusion. This can be implemented in ggplot and it’s largely just a question of getting the data in the right format. For the solution I’ve developed, you need to create a data frame bearing in mind a few simple guidelines.
  • The category column is a factor with the levels in left-to-right order. These will be used to determine the x-axis position.
  • Only the first and last entries in the value column represent the absolute measured units. The other entries should be given as changes relative to the previous column. The waterfall() function will throw a warning if the running total minus the last value doesn’t equal zero.
  • The sector column can be used to create divisions within each bar.
The resulting data frame looks like this: Waterfall data frame The hard work is done by the waterfall() function which I’ve made available as a Gist. Given the data frame described above, this function does some additional manipulation and then returns a ggplot object, which you can then further customize as necessary. The resulting plot is shown below and the full code is available on Github. Carbon emissions embodied in UK trade, drawn with ggplot.

To leave a comment for the author, please follow the link and comment on their blog: James Keirstead » Rstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)