Waterfall charts using ggplot2 in R

A Waterfall Chart is a form of data visualization which helps in determining the cumulative effect of sequentially introduced positive or negative values – Wikipedia

Waterfall charts can be used in many areas including inventory analysis, profit-loss analysis, and sales analysis.ย Excel is a popular tool used for creating waterfall charts. But R gives us a quick easy way to create these charts!

We are going to visually understand a profit and loss statement by creating a waterfall chart. Letโ€™s first create a dataset in R, which talks about the different sources of income and cost.

we

Now letโ€™s proceed with some data preparation steps in R.

  • We convert desc to factor type
  • We then create a new column called type which describes the different types of cash flows(in-flow, out-flow or total net income)

wer

Next we create two columns called start and end.

  • end is the cumulative sum of the amount.
  • start is the end variable with a lag.

wert

We are going to use the function ggplot, to plot the different metrics of a P&L statement (balance dataset which we have created).It also gives us the range for every type of cash flow (in,out or net)

werty

wertyu

Letโ€™s first look at the various components of a ggplot to understand better:

  • data refers to the dataset that you are looking to visualize
  • mapping is the aesthetic mapping that describe the relationship between the variables & the visual attributes
  • geom ,short for geometric objects, describes the type of plot eg.lines,points,etc
  • stat is very useful for transforming your data before plotting. For e.g. bin data, get quantiles etc.
  • facet helps to display subsets of data in different panels.
  • scales helps to control the mapping between data and aesthetics

The beauty of plotting charts using ggplot is that we can add functions as layers. we can add layers to our chart using the โ€œ+โ€ symbol. For example in the chart shown above,we have used the ggplot function to first plot desc variable.Next,we have added another layer using the geom_rect which specifies that the type of chart is rectangles. Also we have specified what values these rectangular bars should take using the variables ymin and ymax (ymin=end, ymax=start)

So hereโ€™s your waterfall chart! You can also refer to the following link to add different kinds of layers to your chart.

https://docs.ggplot2.org/current/

Related Articles:

ย Examples of How R is Used
Stringi Package in R

Interested in learning about other Analytics and Big Data tools and techniques? Click on our course links and explore more.

Jigsawโ€™s Data Science with SAS Course –ย click here.
Jigsawโ€™sย Data Science with R Courseย –ย click here.
Jigsaw’s Big Data Course – click here.

Related Articles

loader
Please wait while your application is being created.
Request Callback