在ggplot中绘制一个简单的转换漏斗 [英] Plot a simple conversion funnel in ggplot

查看:175
本文介绍了在ggplot中绘制一个简单的转换漏斗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的数据框,如下所示:

  df 
步数数率
1点击332835 100.000000
2注册157697 47.379933
3购物车29866 8.973215
4购买17012 5.111241

如何绘制一个简单的转换漏斗而不是条形图? 如果您必须执行漏斗这只是条形图上的一个变种:

  library(ggplot2)

#get data
dat< - read.table(text =
步骤数字费率
点击次数332835 100.000000
注册157697 47.379933
购物车29866 8.973215
购买17012 5.111241 ,
header = T)

#添加间距,融化,排序
total < - subset(dat,rate == 100)$ numbers
dat $ padding < - (总 - 数字$数字)/ 2
熔化< - 熔化(dat [,-3],id.var ='steps')
熔化< - 熔化$ variable,递减= T),]
mol (熔化的$步骤,水平= rev(水平(熔化的$步骤)))

ggplot(熔化的,aes(x =步骤))+
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( )+
主题(legend.position ='none')+
labs(x ='stage',y ='volume')



也就是说,没有真正的指向一个漏斗图 - 同样的信息可以用简单的条形图表示,不会大惊小怪:

 #获取数据
dat< - read.table(text =
步数值率
点击次数332835 100.000000
注册157697 47.379933
购物车29866 8.973215
购买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
pre>


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?

解决方案

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

library(ggplot2)

# 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(levels(molten$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')

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

这篇关于在ggplot中绘制一个简单的转换漏斗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆