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: And here’s another example from the Carbon Trust, about carbon flows and the UK economy. 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: 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.