Waterfall plots in R

February 18, 2015
By

(This article was first published on James Keirstead » Rstats, and kindly contributed to R-bloggers)

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

Sponsors

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)