Black Lives Matter. Please consider donating to Black Girls Code today.

# Funnel Charts in R

How to create a Funnel Chart in R with Plotly

Building AI apps or dashboards in R? Deploy them to Dash Enterprise for hyper-scalability and pixel-perfect aesthetic.
10% of the Fortune 500 uses Dash Enterprise to productionize AI & data science apps. Find out if your company is using Dash Enterprise

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.

### Introduction

Funnel charts are often used to represent data in different stages of a business process. It’s an important mechanism in Business Intelligence to identify potential problem areas of a process. For example, it’s used to observe the revenue or loss in a sales process for each stage, and displays values that are decreasing progressively. Each stage is illustrated as a percentage of the total of all values.

### Basic Funnel Plot

# Need to install plotly from Github to get funnel plots
# devtools::install_github("ropensci/plotly")
library(plotly)

fig <- plot_ly()
fig <- fig %>%
type = "funnel",
x = c(39, 27.4, 20.6, 11, 2))
fig <- fig %>%
layout(yaxis = list(categoryarray = c("Website visit", "Downloads", "Potential customers", "Requested price", "invoice sent")))

fig


### Setting Marker Size and Color

This example uses textposition and textinfo to determine information apears on the graph, and shows how to customize the bars.

# Need to install plotly from Github to get funnel plots
# devtools::install_github("ropensci/plotly")
library(plotly)

fig <- plot_ly()
fig <- fig %>%
x = c(39, 27.4, 20.6, 11, 2),
textposition = "inside",
textinfo = "value+percent initial",
opacity = 0.65,
marker = list(color = c("deepskyblue", "lightsalmon", "tan", "teal", "silver"),
line = list(width = c(4, 2, 2, 3, 1, 1), color = c("wheat", "wheat", "blue", "wheat", "wheat"))),
connector = list(line = list(color = "royalblue", dash = "dot", width = 3)))
fig <- fig %>%
layout(yaxis = list(categoryarray = c("Website visit", "Downloads", "Potential customers", "Requested price", "Finalized")))

fig


### Stacked Funnel Plot

# Need to install plotly from Github to get funnel plots
# devtools::install_github("ropensci/plotly")
library(plotly)

fig <- plot_ly(
type = "funnel",
name = 'Montreal',
x = c(120, 60, 30, 20),
textinfo = "value+percent initial")
fig <- fig %>%
type = "funnel",
name = 'Toronto',
orientation = "h",
x = c(100, 60, 40, 30, 20),
textposition = "inside",
textinfo = "value+percent previous")
fig <- fig %>%
type = "funnel",
name = 'Vancouver',
orientation = "h",
y = c("Website visit", "Downloads", "Potential customers", "Requested price", "invoice sent", "Finalized"),
x = c(90, 70, 50, 30, 10, 5),
textposition = "outside",
textinfo = "value+percent total")
fig <- fig %>%
layout(yaxis = list(categoryarray = c("Website visit", "Downloads", "Potential customers", "Requested price", "invoice sent", "Finalized")))

fig


### Basic Area Funnel Plot

# Need to install plotly from Github to get funnel plots
# devtools::install_github("ropensci/plotly")
library(plotly)

fig <- plot_ly(
type = "funnelarea",
text = c("The 1st","The 2nd", "The 3rd", "The 4th", "The 5th"),
values = c(5, 4, 3, 2, 1))

fig


### Set Marker Size and Color in Area Funnel Plots

# Need to install plotly from Github to get funnel plots
# devtools::install_github("ropensci/plotly")
library(plotly)

fig <- plot_ly(
type = "funnelarea",
values = c(5, 4, 3, 2, 1),
text = c("The 1st","The 2nd", "The 3rd", "The 4th", "The 5th"),
marker = list(colors = c("deepskyblue", "lightsalmon", "tan", "teal", "silver"),
line = list(color = c("wheat", "wheat", "blue", "wheat", "wheat"), width = c(0, 1, 5, 0, 4))),
textfont = list(family = "Old Standard TT, serif", size = 13, color = "black"),
opacity = 0.65)

fig


### Multiple Area Funnels

# Need to install plotly from Github to get funnel plots
# devtools::install_github("ropensci/plotly")
library(plotly)

fig <- plot_ly(
type = "funnelarea",
scalegroup = "first",
values = c(500, 450, 340, 230, 220, 110),
textinfo = "value",
title = list(position = "top center", text = "Sales for Sale Person A in U.S."),
domain = list(x = c(0.01, 0.48), y =c(0, 0.5)))
fig <- fig %>%
type = "funnelarea",
scalegroup = "first",
values = c(600, 500, 400, 300, 200, 100),
textinfo = "value",
title = list(position = "top center", text = "Sales of Sale Person B in Canada"),
domain = list(x = c(0.01, 0.48), y = c(0.56, 1)))
fig <- fig %>%
type = "funnelarea",
scalegroup = "second",
values = c(510, 480, 440, 330, 220, 100),
textinfo = "value",
title = list(position = "top left", text = "Sales of Sale Person A in Canada"),
domain = list(x = c(0.56, 0.98), y = c(0, 0.5)))
fig <- fig %>%
type = "funnelarea",
scalegroup = "second",
values = c(360, 250, 240, 130, 120, 60),
textinfo = "value",
title = list(position = "top left", text = "Sales of Sale Person B in U.S."),
domain = list(x = c(0.56, 0.98), y = c(0.56, 1)))
fig <- fig %>%
layout(
margin = list(l= 200, r= 200), shapes = list(
list(x0 = 0, x1 = 0.5, y0 = 0, y1 = 0.5),
list(x0 = 0, x1 = 0.5, y0 = 0.55, y1 = 1),
list(x0 = 0.55, x1 = 1, y0 = 0, y1 = 0.5),
list(x0 = 0.55, x1 = 1, y0 = 0.55, y1 = 1)))

fig


# Reference

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