繁体   English   中英

如何修复销售漏斗的ggplot2代码?

[英]How to fix ggplot2 code for sales funnel?

我试图运行这里提到的代码:

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 %>%
select(step, maxx, minx) %>%
group_by(step) %>%
unique()

# data frame with dummies
df.dum <- df.all %>%
select(step, dum) %>%
unique() %>%
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 %>%
select(step, totnum) %>%
group_by(step) %>%
unique() %>%
ungroup() %>%
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)

df.all <- df.all %>%
group_by(step) %>%
arrange(desc(content)) %>%
ungroup()

# calculating position of labels
df.all <- df.all %>%
group_by(step) %>%
mutate(pos = cumsum(number) - 0.5*number)

# defining order of steps
df.all$step <- factor(df.all$step, levels = c('loyal', 'engaged', 'new', 'action', 'desire', 'interest', 'awareness'))
list <- c(unique(as.character(df.all$content)))
df.all$content <- factor(df.all$content, levels = c('dummy', c(list)))

# creating custom palette with 'white' color for dummies
cols <- c("#ffffff", "#fec44f", "#fc9272", "#a1d99b", "#fee0d2", "#2ca25f",
"#8856a7", "#43a2ca", "#fdbb84", "#e34a33",
"#a6bddb", "#dd1c77", "#ffeda0", "#756bb1")

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

由于出现重复的因子错误:

Warning message:
In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated

结果是这个丑陋的情节 在此处输入图片说明

我更改了这段代码:

df.all$content <- factor(df.all$content, levels = c('dummy', c(list)))

成:

df.all$content <- factor(df.all$content, levels = c(list)))

结果,我得到这个情节: 在此处输入图片说明

根据博客的作者,这应该是结果:

在此处输入图片说明

我认为我不必解释我的情节不是应该的。 我还不能弄清楚是什么原因引起的。 有谁知道这里发生了什么以及如何解决?

问题是每个栏中的组的顺序是..well,顺序混乱。

可能由于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_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())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM