Funnel Chart in ggplot2

How to make Funnel Chart in ggplot2 with Plotly.


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Funnel plot example

library(plotly)
library(ggplot2)
library(tidyverse)

STAGE <- c('Stage 01: Browsers','Stage 02: Unbounced Users','Stage 03: Email Signups','Stage 04: Email Confirmed','Stage 05: Campaign-Email Opens','Stage 06: Campaign-Email Clickthroughs','Stage 07: Buy Button Page','Stage 08: Buy Button Clickers','Stage 09: Cart Confirmation Page','Stage 10: Address Verification Page','Stage 11: Submit Order Page','Stage 12: Payment','Stage 13: Payment Successful','Stage 14: 1st Successful Purchase','Stage 15: 2nd Purchase','Stage 16: 3rd Purchase','Stage 17: 4th Purchase','Stage 18: 5th Purchase','Stage 01: Browsers','Stage 02: Unbounced Users','Stage 03: Email Signups','Stage 04: Email Confirmed','Stage 05: Campaign-Email Opens','Stage 06: Campaign-Email Clickthroughs','Stage 07: Buy Button Page','Stage 08: Buy Button Clickers','Stage 09: Cart Confirmation Page','Stage 10: Address Verification Page','Stage 11: Submit Order Page','Stage 12: Payment','Stage 13: Payment Successful','Stage 14: 1st Successful Purchase','Stage 15: 2nd Purchase','Stage 16: 3rd Purchase','Stage 17: 4th Purchase','Stage 18: 5th Purchase')

GENDER <- c('Male','Male','Male','Male','Male','Male','Male','Male','Male','Male','Male','Male','Male','Male','Male','Male','Male','Male','Female','Female','Female','Female','Female','Female','Female','Female','Female','Female','Female','Female','Female','Female','Female','Female','Female','Female')

USERS <- c(-14927618.71,-12862663.41,-11361896.41,-9411708.103,-8074316.616,-6958512.218,-6045363.483,-5029954.214,-4008034.113,-3172555.225,-2484808.199,-1903727.481,-1490277.016,-1152003.965,-770748.0581,-434430.0282,-195031.8899,-58570.22156,14226434.29,12276042.59,10850385.59,8999931.897,7732693.384,6666393.782,5743259.517,4723254.786,3680878.887,3002640.775,2467804.801,1977277.519,1593649.984,1229651.035,828496.9419,486621.9718,227106.1101,73466.77844)


df <- data.frame(STAGE, GENDER, USERS)

brks <- c(seq(-15000000, 15000000, by = 5000000))
lbls = c(seq(15, 0, -5), seq(5, 15, 5))

p <- df %>% mutate(USERS = as.numeric(USERS)) %>%
              ggplot(aes(x = reorder(STAGE,abs(USERS)), y = USERS, fill = GENDER)) +
              geom_bar(stat = "identity", width = .6) +
              scale_y_continuous(breaks = brks, labels = lbls) +
              coord_flip() +
              theme_minimal() +
              labs(title="Email Campaign Funnel") +
              theme(plot.title = element_text(hjust = .5),
                                    axis.ticks = element_blank())

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)