R 如何修复销售漏斗的ggplot2代码?
我尝试运行上面提到的代码:R 如何修复销售漏斗的ggplot2代码?,r,ggplot2,R,Ggplot2,我尝试运行上面提到的代码: 库(dplyr) 图书馆(GG2) 图书馆(E2) #创建一个数据示例 #内容 df.content% 突变(dum=(max(totnum)-totnum)/2, maxx=totnum+dum, minx=dum) #绘制漏斗线的数据框 df.line% 选择(步长、最大值、最小值)%>% 分组依据(步进)%>% 唯一的() #带假人的数据帧 df.dum% 选择(步骤,dum)%>% 唯一()%>% 变异(内容='dummy', 数量=dum)%>% 选择(内容
库(dplyr)
图书馆(GG2)
图书馆(E2)
#创建一个数据示例
#内容
df.content%
突变(dum=(max(totnum)-totnum)/2,
maxx=totnum+dum,
minx=dum)
#绘制漏斗线的数据框
df.line%
选择(步长、最大值、最小值)%>%
分组依据(步进)%>%
唯一的()
#带假人的数据帧
df.dum%
选择(步骤,dum)%>%
唯一()%>%
变异(内容='dummy',
数量=dum)%>%
选择(内容、步骤、编号)
#带速率的数据帧
conv%
分组依据(步进)%>%
唯一()%>%
解组()%>%
突变(prevnum=滞后(totnum),
速率=ifelse(步骤=='新'|步骤=='参与'|步骤=='忠诚',
四舍五入(总计/转换,3),
四舍五入(总数/前置数,3))%>%
选择(步骤、速率)
df.费率%
解组()
#计算标签的位置
df.all%
分组依据(步进)%>%
突变(位置=累积数(数字)-0.5*数字)
#定义步骤顺序
df.all$step问题在于,每个条中的组的顺序是..嗯,无序的
可能是由于排序因子ggplot
发生了一些变化,无论如何,颠倒了顺序解决了问题:
ggplot() +
theme_minimal() +
coord_flip() +
scale_fill_manual(values=cols) +
geom_bar(data=df.all, aes(x=step, y=number, fill=content), position = position_stack(reverse = T), width=1, stat = 'identity') +
geom_text(data=df.all[df.all$content!='dummy', ],
aes(x=step, y=pos, label=paste0(content, '-', number/1000, 'K')),
size=4, color='white', fontface="bold") +
geom_ribbon(data=df.lines, aes(x=step, ymax=max(maxx), ymin=maxx, group=1), fill='white') +
geom_line(data=df.lines, aes(x=step, y=maxx, group=1), color='darkred', size=4) +
geom_ribbon(data=df.lines, aes(x=step, ymax=minx, ymin=min(minx), group=1), fill='white') +
geom_line(data=df.lines, aes(x=step, y=minx, group=1), color='darkred', size=4) +
geom_text(data=df.rates, aes(x=step, y=(df.lines$minx[-1]), label=paste0(rate*100, '%')), hjust=1.2,
color='darkblue', fontface="bold") +
theme(legend.position='none', axis.ticks=element_blank(), axis.text.x=element_blank(),
axis.title.x=element_blank())
其中,感兴趣位为:
geom_bar(data=df.all, aes(x=step, y=number, fill=content), position = position_stack(reverse = T), width=1, stat = 'identity')
请注意,这应该更新为使用geom\u col
:
geom_col(data=df.all, aes(x=step, y=number, fill=content), position = position_stack(reverse = T), width=1)
正如@Uwe Block所说,这仍然是一个丑陋且难以理解的问题。更好的方法是使用Sankey图表
结果:
问题是由于软件包中的某些更新。工作代码如下:
library(dplyr)
library(ggplot2)
library(reshape2)
# creating a data samples
# content
df.content <- data.frame(content = c('main', 'ad landing',
'product 1', 'product 2', 'product 3', 'product 4',
'shopping cart',
'thank you page'),
step = c('awareness', 'awareness',
'interest', 'interest', 'interest', 'interest',
'desire',
'action'),
number = c(150000, 80000,
80000, 40000, 35000, 25000,
130000,
120000))
# customers
df.customers <- data.frame(content = c('new', 'engaged', 'loyal'),
step = c('new', 'engaged', 'loyal'),
number = c(25000, 40000, 55000))
# combining two data sets
df.all <- rbind(df.content, df.customers)
# calculating dummies, max and min values of X for plotting
df.all <- df.all %>%
group_by(step) %>%
mutate(totnum = sum(number)) %>%
ungroup() %>%
mutate(dum = (max(totnum) - totnum)/2,
maxx = totnum + dum,
minx = dum)
# data frame for plotting funnel lines
df.lines <- df.all %>%
distinct(step, maxx, minx)
# data frame with dummies
df.dum <- df.all %>%
distinct(step, dum) %>%
mutate(content = 'dummy',
number = dum) %>%
select(content, step, number)
# data frame with rates
conv <- df.all$totnum[df.all$step == 'action']
df.rates <- df.all %>%
distinct(step, totnum) %>%
mutate(prevnum = lag(totnum),
rate = ifelse(step == 'new' | step == 'engaged' | step == 'loyal',
round(totnum / conv, 3),
round(totnum / prevnum, 3))) %>%
select(step, rate)
df.rates <- na.omit(df.rates)
# creting final data frame
df.all <- df.all %>%
select(content, step, number)
df.all <- rbind(df.all, df.dum)
# defining order of steps
df.all$step <- factor(df.all$step, levels = c('loyal', 'engaged', 'new', 'action', 'desire', 'interest', 'awareness'))
df.all <- df.all %>%
arrange(desc(step))
list1 <- df.all %>% distinct(content) %>%
filter(content != 'dummy')
df.all$content <- factor(df.all$content, levels = c(as.character(list1$content), 'dummy'))
# calculating position of labels
df.all <- df.all %>%
arrange(step, desc(content)) %>%
group_by(step) %>%
mutate(pos = cumsum(number) - 0.5*number) %>%
ungroup()
# creating custom palette with 'white' color for dummies
cols <- c("#fec44f", "#fc9272", "#a1d99b", "#fee0d2",
"#2ca25f", "#8856a7", "#43a2ca", "#fdbb84",
"#e34a33", "#a6bddb", "#dd1c77", "#ffffff")
# plotting chart
ggplot() +
theme_minimal() +
coord_flip() +
scale_fill_manual(values=cols) +
geom_bar(data=df.all, aes(x=step, y=number, fill=content), stat="identity", width=1) +
geom_text(data=df.all[df.all$content!='dummy', ],
aes(x=step, y=pos, label=paste0(content, '-', number/1000, 'K')),
size=4, color='white', fontface="bold") +
geom_ribbon(data=df.lines, aes(x=step, ymax=max(maxx), ymin=maxx, group=1), fill='white') +
geom_line(data=df.lines, aes(x=step, y=maxx, group=1), color='darkred', size=4) +
geom_ribbon(data=df.lines, aes(x=step, ymax=minx, ymin=min(minx), group=1), fill='white') +
geom_line(data=df.lines, aes(x=step, y=minx, group=1), color='darkred', size=4) +
geom_text(data=df.rates, aes(x=step, y=(df.lines$minx[-1]), label=paste0(rate*100, '%')), hjust=1.2,
color='darkblue', fontface="bold") +
theme(legend.position='none', axis.ticks=element_blank(), axis.text.x=element_blank(),
axis.title.x=element_blank())
库(dplyr)
图书馆(GG2)
图书馆(E2)
#创建一个数据示例
#内容
df.content%
突变(dum=(max(totnum)-totnum)/2,
maxx=totnum+dum,
minx=dum)
#绘制漏斗线的数据框
df.line%
不同(步长、最大值、最小值)
#带假人的数据帧
df.dum%
不同(阶跃,dum)%%>%
变异(内容='dummy',
数量=dum)%>%
选择(内容、步骤、编号)
#带速率的数据帧
conv%
突变(prevnum=滞后(totnum),
速率=ifelse(步骤=='新'|步骤=='参与'|步骤=='忠诚',
四舍五入(总计/转换,3),
四舍五入(总数/前置数,3))%>%
选择(步骤、速率)
df.费率%
解组()
#为假人创建带有“白色”颜色的自定义调色板
科尔斯我很抱歉地说,这是大量的代码来创建一个糟糕的图形,这是很难阅读和理解。它试图很难模仿谚语中的“销售漏斗”,从而隐藏重要信息。顺便说一句,结果看起来更像一个高脚杯,而不是一个漏斗。我试图了解这些条的宽度与K值标签或百分比的关系。不管怎么说,看起来联系已经破裂了。检查每个因素的水平,它们的顺序是否正确?您应该根据决定变量的级别设置每个变量的级别。@R.S.百分比是此图形设计中的一个特别的坏缺陷。前三个值是总计数与前一步的份额。最后三个值是步骤操作中计数的份额。新员工、敬业员工和忠诚员工总计100%或120K绝对员工。因此,显示的百分比有不同的含义,不应该在一条曲线中显示。这里的基本问题是,两个不同的衡量标准,销售漏斗和客户忠诚度,被混合在一起,得到了一个看起来“花哨”的图表。相关帖子最好描述你的确切问题。换句话说,如果你有同样的问题,你会搜索什么。我相信这不仅仅是“如何修复ggplot2?”。谢谢。我用你的代码替换了我的ggplot代码,但你的代码似乎也不起作用。你确定你得到了预期的图吗?@GGamba+1 esp用于你建议作为备选方案的Sankey图
ggplot() +
theme_minimal() +
coord_flip() +
scale_fill_manual(values=cols) +
geom_bar(data=df.all, aes(x=step, y=number, fill=content), position = position_stack(reverse = T), width=1, stat = 'identity') +
geom_text(data=df.all[df.all$content!='dummy', ],
aes(x=step, y=pos, label=paste0(content, '-', number/1000, 'K')),
size=4, color='white', fontface="bold") +
geom_ribbon(data=df.lines, aes(x=step, ymax=max(maxx), ymin=maxx, group=1), fill='white') +
geom_line(data=df.lines, aes(x=step, y=maxx, group=1), color='darkred', size=4) +
geom_ribbon(data=df.lines, aes(x=step, ymax=minx, ymin=min(minx), group=1), fill='white') +
geom_line(data=df.lines, aes(x=step, y=minx, group=1), color='darkred', size=4) +
geom_text(data=df.rates, aes(x=step, y=(df.lines$minx[-1]), label=paste0(rate*100, '%')), hjust=1.2,
color='darkblue', fontface="bold") +
theme(legend.position='none', axis.ticks=element_blank(), axis.text.x=element_blank(),
axis.title.x=element_blank())
geom_bar(data=df.all, aes(x=step, y=number, fill=content), position = position_stack(reverse = T), width=1, stat = 'identity')
geom_col(data=df.all, aes(x=step, y=number, fill=content), position = position_stack(reverse = T), width=1)
library(dplyr)
library(ggplot2)
library(reshape2)
# creating a data samples
# content
df.content <- data.frame(content = c('main', 'ad landing',
'product 1', 'product 2', 'product 3', 'product 4',
'shopping cart',
'thank you page'),
step = c('awareness', 'awareness',
'interest', 'interest', 'interest', 'interest',
'desire',
'action'),
number = c(150000, 80000,
80000, 40000, 35000, 25000,
130000,
120000))
# customers
df.customers <- data.frame(content = c('new', 'engaged', 'loyal'),
step = c('new', 'engaged', 'loyal'),
number = c(25000, 40000, 55000))
# combining two data sets
df.all <- rbind(df.content, df.customers)
# calculating dummies, max and min values of X for plotting
df.all <- df.all %>%
group_by(step) %>%
mutate(totnum = sum(number)) %>%
ungroup() %>%
mutate(dum = (max(totnum) - totnum)/2,
maxx = totnum + dum,
minx = dum)
# data frame for plotting funnel lines
df.lines <- df.all %>%
distinct(step, maxx, minx)
# data frame with dummies
df.dum <- df.all %>%
distinct(step, dum) %>%
mutate(content = 'dummy',
number = dum) %>%
select(content, step, number)
# data frame with rates
conv <- df.all$totnum[df.all$step == 'action']
df.rates <- df.all %>%
distinct(step, totnum) %>%
mutate(prevnum = lag(totnum),
rate = ifelse(step == 'new' | step == 'engaged' | step == 'loyal',
round(totnum / conv, 3),
round(totnum / prevnum, 3))) %>%
select(step, rate)
df.rates <- na.omit(df.rates)
# creting final data frame
df.all <- df.all %>%
select(content, step, number)
df.all <- rbind(df.all, df.dum)
# defining order of steps
df.all$step <- factor(df.all$step, levels = c('loyal', 'engaged', 'new', 'action', 'desire', 'interest', 'awareness'))
df.all <- df.all %>%
arrange(desc(step))
list1 <- df.all %>% distinct(content) %>%
filter(content != 'dummy')
df.all$content <- factor(df.all$content, levels = c(as.character(list1$content), 'dummy'))
# calculating position of labels
df.all <- df.all %>%
arrange(step, desc(content)) %>%
group_by(step) %>%
mutate(pos = cumsum(number) - 0.5*number) %>%
ungroup()
# creating custom palette with 'white' color for dummies
cols <- c("#fec44f", "#fc9272", "#a1d99b", "#fee0d2",
"#2ca25f", "#8856a7", "#43a2ca", "#fdbb84",
"#e34a33", "#a6bddb", "#dd1c77", "#ffffff")
# plotting chart
ggplot() +
theme_minimal() +
coord_flip() +
scale_fill_manual(values=cols) +
geom_bar(data=df.all, aes(x=step, y=number, fill=content), stat="identity", width=1) +
geom_text(data=df.all[df.all$content!='dummy', ],
aes(x=step, y=pos, label=paste0(content, '-', number/1000, 'K')),
size=4, color='white', fontface="bold") +
geom_ribbon(data=df.lines, aes(x=step, ymax=max(maxx), ymin=maxx, group=1), fill='white') +
geom_line(data=df.lines, aes(x=step, y=maxx, group=1), color='darkred', size=4) +
geom_ribbon(data=df.lines, aes(x=step, ymax=minx, ymin=min(minx), group=1), fill='white') +
geom_line(data=df.lines, aes(x=step, y=minx, group=1), color='darkred', size=4) +
geom_text(data=df.rates, aes(x=step, y=(df.lines$minx[-1]), label=paste0(rate*100, '%')), hjust=1.2,
color='darkblue', fontface="bold") +
theme(legend.position='none', axis.ticks=element_blank(), axis.text.x=element_blank(),
axis.title.x=element_blank())