版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Cocaine_bai/article/details/80492155
《数据分析实战》–用R做柱状图
本文参考的是《数据分析实战》
背景:针对某公司的产品,发现当月的销售额下降,但从市场环境还是从游戏本身的状态来看都有上升的空间,请查明下降的原因。
现状:同上月相比销售额下降
预期:销售额恢复到同上月的水平
明确问题:由于公司的预算问题,当月没有进行过多的宣传活动,所以很有可能是该原因导致。
读取数据
dau <- read.csv('section3-dau.csv',header = T,stringsAsFactors = F)
dpu <- read.csv('section3-dpu.csv',header = T,stringsAsFactors = F)
install <- read.csv('section3-install.csv', header = T,stringsAsFactors = F)
其中dau数据如下:
log_data | app_name | user_id |
---|---|---|
2013-06-01 | game-01 | 116 |
2013-06-01 | game-01 | 13491 |
2013-06-01 | game-01 | 7006 |
2013-06-01 | game-01 | 117 |
2013-06-01 | game-01 | 13492 |
2013-06-01 | game-01 | 9651 |
2013-06-01 | game-01 | 1 |
2013-06-01 | game-01 | 3 |
2013-06-01 | game-01 | 6 |
2013-06-01 | game-01 | 11 |
….. | ….. | ….. |
其中dpu数据如下:
log_date | app_name | user_id | payment |
---|---|---|---|
2013-06-01 | game-01 | 351 | 1333 |
2013-06-01 | game-01 | 12796 | 81 |
2013-06-01 | game-01 | 364 | 571 |
2013-06-01 | game-01 | 13212 | 648 |
2013-06-01 | game-01 | 13212 | 1142 |
2013-06-01 | game-01 | 13212 | 571 |
2013-06-01 | game-01 | 13212 | 1333 |
2013-06-01 | game-01 | 3547 | 571 |
2013-06-01 | game-01 | 12644 | 81 |
2013-06-01 | game-01 | 19 | 162 |
….. | …… | …… | ….. |
其中install数据如下:
install_date | app_name | user_id |
---|---|---|
2013-04-15 | game-01 | 1 |
2013-04-15 | game-01 | 2 |
2013-04-15 | game-01 | 3 |
2013-04-15 | game-01 | 4 |
2013-04-15 | game-01 | 5 |
2013-04-15 | game-01 | 6 |
2013-04-15 | game-01 | 7 |
2013-04-15 | game-01 | 8 |
2013-04-15 | game-01 | 9 |
2013-04-15 | game-01 | 10 |
…… | …… | …… |
处理数据
将三张表合并成一张表(使用merge函数):
dau_install <- merge(dau,install,by = c('user_id','app_name'))
dau_install_payment <- merge(dau_install,dpu, by=c('log_date', 'app_name','user_id'),all.x = T)
合并之后数据如下:
log_date | app_name | user_id | install_date | payment |
---|---|---|---|---|
2013-06-01 | game-01 | 1 | 2013-04-15 | NA |
2013-06-01 | game-01 | 3 | 2013-04-15 | NA |
2013-06-01 | game-01 | 6 | 2013-04-15 | NA |
2013-06-01 | game-01 | 11 | 2013-04-15 | NA |
2013-06-01 | game-01 | 17 | 2013-04-15 | NA |
2013-06-01 | game-01 | 18 | 2013-04-15 | NA |
…… | …… | …… | …… | ….. |
进行数据处理,对NA值赋值为0:
dau_install_payment$payment[is.na(dau_install_payment$payment)]<- 0
增加月份统计:
dau_install_payment$log_month <- substr(dau_install_payment$log_date,1,7)
dau_install_payment$install_month <- substr(dau_install_payment$install_date,1,7)
library(plyr)
mau_payment <- ddply(dau_install_payment,
.(log_month,user_id,install_month),
summarise,
payment = sum(payment))
月份统计结果:
log_month | user_id | install_month | payment |
---|---|---|---|
2013-06 | 1 | 2013-04 | 0 |
2013-06 | 2 | 2013-04 | 0 |
2013-06 | 3 | 2013-04 | 14994 |
2013-06 | 4 | 2013-04 | 0 |
2013-06 | 6 | 2013-04 | 0 |
2013-06 | 7 | 2013-04 | 0 |
2013-06 | 8 | 2013-04 | 0 |
2013-06 | 10 | 2013-04 | 0 |
2013-06 | 11 | 2013-04 | 1937 |
2013-06 | 15 | 2013-04 | 0 |
….. | ….. | ….. | ….. |
现在数据已经处理的差不多了,感觉plyr包中ddply函数有点像Excel的数据透视表,根据log_month,user_id,install_month在进行合并汇总。
不过数据处理有很多种方法,大家可以试一试sqldf包中sqldf函数,里面直接写SQL就OK了,简单方便。
数据分析
对上面的mau_payment 增加字段,让我们来识别新用户和老用户:
如果安装的月份和登陆的月份相同,我们则认为他是本月的新用户,否则为老用户
mau_payment$user_type <- ifelse(mau_payment$log_month == mau_payment$install_month,
'new_user','old_user')
mau_payment_summary <- ddply(mau_payment,
.(log_month,user_type),
summarise,
total_payment = sum(payment))
head(mau_payment_summary)
数据结果如下:
log_month | user_type | total_payment |
---|---|---|
2013-06 | new_user | 49837 |
2013-06 | old_user | 177886 |
2013-07 | new_user | 29199 |
2013-07 | old_user | 177886 |
从上面的表格里面可以看出:在7月份中,新用户带来的消费额明显的降低而导致7月份的整体消费额降低。
所以:宣传活动需要保持6月份的水平才能让消费额回升至6月份的水平。
数据可视化
library(ggplot2)
library(scales)
ggplot(mau_payment_summary, aes(x = log_month,y=total_payment,
fill = user_type)) + geom_bar(stat = "identity") + scale_y_continuous(label = comma)
其中:ggplot2是作图的包,
scales包主要将普通数字转换为以三位数为一个区间来显示的工具,例如1000 经转换后为1,000。
绘图如下:
可以很明显的看到,新用户带来的收入下降。
那么怎么看哪个消费层次的的消费额减少了呢?
ggplot(mau_payment[mau_payment$payment >0 & mau_payment$user_type == 'new_user',],
aes(x=payment,fill=log_month))+geom_histogram(position = 'dodge',binwidth = 2000)
至此,数据分析基本完成~