Waterfall Charts in ggplot2

How to make Waterfall Charts in ggplot2 with Plotly.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.

Default waterfall plot

library(plotly)
library(ggplot2)

balance <- data.frame(desc = c("Starting Cash",
     "Sales", "Refunds", "Payouts", "Court Losses",
     "Court Wins", "Contracts", "End Cash"), amount = c(2000,
     3400, -1100, -100, -6600, 3800, 1400, 2800))

# In order to preserve the order of the lines in a dataframe I convert the desc variable to a factor; id and type variable are also added:
balance$desc <- factor(balance$desc, levels = balance$desc)
balance$id <- seq_along(balance$amount)
balance$type <- ifelse(balance$amount > 0, "in","out")
balance[balance$desc %in% c("Starting Cash", "End Cash"), "type"] <- "net"

# Next the data will be slightly reworked to specify the coordinates for drawing the waterfall bars.
balance$end <- cumsum(balance$amount)
balance$end <- c(head(balance$end, -1), 0)
balance$start <- c(0, head(balance$end, -1))
balance <- balance[, c(3, 1, 4, 6, 5, 2)]

p <- ggplot(balance, aes(desc, fill = type)) + geom_rect(aes(x = desc,
     xmin = id - 0.45, xmax = id + 0.45, ymin = end,
     ymax = start))

ggplotly(p)

Adjusting colours and labels

The fill mapping could use some tweaking (for example: outflows in red, inflows in green, and net position in blue), for that change the order of the underlying factor levels.

To improve readability of the legend add the following function strwr <- function(str) gsub(" ", "\n", str).

library(plotly)
library(ggplot2)

balance <- data.frame(desc = c("Starting Cash",
     "Sales", "Refunds", "Payouts", "Court Losses",
     "Court Wins", "Contracts", "End Cash"), amount = c(2000,
     3400, -1100, -100, -6600, 3800, 1400, 2800))
# In order to preserve the order of the lines in a dataframe I convert the desc variable to a factor; id and type variable are also added:
balance$desc <- factor(balance$desc, levels = balance$desc)
balance$id <- seq_along(balance$amount)
balance$type <- ifelse(balance$amount > 0, "in","out")
balance$type <- factor(balance$type, levels = c("out","in", "net"))
balance[balance$desc %in% c("Starting Cash", "End Cash"), "type"] <- "net"

# Next the data will be slightly reworked to specify the coordinates for drawing the waterfall bars.
balance$end <- cumsum(balance$amount)
balance$end <- c(head(balance$end, -1), 0)
balance$start <- c(0, head(balance$end, -1))
balance <- balance[, c(3, 1, 4, 6, 5, 2)]

strwr <- function(str) gsub(" ", "\n", str)

p <- ggplot(balance, aes(fill = type)) + geom_rect(aes(x = desc,
    xmin = id - 0.45, xmax = id + 0.45, ymin = end,
    ymax = start)) + 
    scale_x_discrete("", breaks = levels(balance$desc),
        labels = strwr(levels(balance$desc)))
ggplotly(p)

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)