Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot a simple conversion funnel in ggplot

Tags:

r

ggplot2

I have a simple dataframe that looks like this:

df
   steps  numbers     rate
 1 clicks 332835  100.000000
 2 signup  157697  47.379933
 3  cart   29866   8.973215
 4  buys   17012   5.111241

How can I plot a simple conversion funnel instead of a barchart?

like image 533
anonymous Avatar asked Mar 07 '16 22:03

anonymous


2 Answers

If you must do the funnel thing, it's just a variation on bar chart:

library(ggplot2)
library(reshape2) # for melt()

# get data
dat <- read.table(text=
"steps  numbers     rate
clicks 332835  100.000000
signup  157697  47.379933
cart   29866   8.973215
buys   17012   5.111241", 
header = T)

# add spacing, melt, sort
total <- subset(dat, rate==100)$numbers
dat$padding <- (total - dat$numbers) / 2
molten <- melt(dat[, -3], id.var='steps')
molten <- molten[order(molten$variable, decreasing = T), ]
molten$steps <- factor(molten$steps, levels = rev(dat$steps))

ggplot(molten, aes(x=steps)) +
  geom_bar(aes(y = value, fill = variable),
           stat='identity', position='stack') +
  geom_text(data=dat, 
            aes(y=total/2, label= paste(round(rate), '%')),
            color='white') +
  scale_fill_manual(values = c('grey40', NA) ) +
  coord_flip() +
  theme(legend.position = 'none') +
  labs(x='stage', y='volume')

enter image description here

That said, there's no real point in a "funnel chart" - the same information can be presented in a plain bar chart with less fuss:

# get data
dat <- read.table(text=
"steps  numbers     rate
clicks 332835  100.000000
signup  157697  47.379933
cart   29866   8.973215
buys   17012   5.111241", 
header = T)

# order x axis
dat$steps <- factor(dat$steps, levels = dat$steps)

# plot
ggplot(dat, aes(x=steps, y=numbers)) +
  geom_bar(stat='identity') +
  geom_text(aes(label = paste(round(rate), '%')), vjust=-0.5

enter image description here

like image 89
arvi1000 Avatar answered Nov 08 '22 23:11

arvi1000


Alternatively, you can do a simple funnel in highcharts. My dataframe looks like this:

# data is a df called check_stage
check_stage

# A tibble: 9 × 4
  stage_name               count x     percent
  <ord>                    <int> <chr>   <dbl>
1 Opportunity Disqualified   805 1        13.5
2 Qualifying                5138 2        86.5
3 Evaluation                1773 3        29.8
4 Meeting Scheduled         4104 4        69.1
5 Quoted                    4976 5        83.7
6 Order Submitted           1673 6        28.2
7 Closed Won                1413 7        23.8
8 Closed Lost                957 8        16.1
9 Nurture                   1222 9        20.6

library(highcharter)

# make a funnel plot of stage
options(highcharter.theme = hc_theme_smpl(tooltip = list(valueDecimals = 1,
                                                         pointFormat = "{point.y}%")))


hc <- check_stage %>%
  hchart(
    "funnel", hcaes(x = stage_name, y = percent), name = "Proportion of Leads")

hc

enter image description here

like image 45
Bryan Butler Avatar answered Nov 08 '22 23:11

Bryan Butler