Funnel Charts in R
How to create a Funnel Chart in R with Plotly
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.
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 %>%
add_trace(
type = "funnel",
y = c("Website visit", "Downloads", "Potential customers", "Requested price", "invoice sent"),
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
Click to copy
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 %>%
add_trace(type = "funnel",
y = c("Website visit", "Downloads", "Potential customers", "Requested price", "Finalized"),
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
Click to copy
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',
y = c("Website visit", "Downloads", "Potential customers", "Requested price"),
x = c(120, 60, 30, 20),
textinfo = "value+percent initial")
fig <- fig %>%
add_trace(
type = "funnel",
name = 'Toronto',
orientation = "h",
y = c("Website visit", "Downloads", "Potential customers", "Requested price", "invoice sent"),
x = c(100, 60, 40, 30, 20),
textposition = "inside",
textinfo = "value+percent previous")
fig <- fig %>%
add_trace(
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
Click to copy
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
Click to copy
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
Click to copy
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 %>%
add_trace(
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 %>%
add_trace(
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 %>%
add_trace(
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
Click to copy
Reference
See https://plotly.com/r/reference/#funnel and https://plotly.com/r/reference/#funnelarea for more information and chart attribute options!
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)
Click to copy