ggplot2-快速研究数据分布

拿到数据,第一步想要了解就是数据的分布,ggplot2的可视化观察样本的分布十分之方便。

# 加载package
library(ggplot2);library(ggtheme) # 画图
library(data.table) # 数据处理
##### 样例数据格式 ######
plot.data
         elo      Day        Time
     1:  344  2016-06-02   00:07:41
     2: 1285  2016-06-02   00:09:49
     3: 2006  2016-06-02   00:12:35
     4: 7355  2016-06-02   00:16:43
     5: 1820  2016-06-02   00:17:26
    ---                          
309206: 1522  2016-06-06   23:59:19
309207:  600  2016-06-06   23:59:12
309208:  981  2016-06-06   23:59:29
309209:  174  2016-06-06   23:59:40
309210:   76  2016-06-06   23:59:35

观察elo总体分布

有几种图型观察方式,线图、点图、面积图,对应: geom_line, geom_point, geom_area。
以geom_line为例其他图形更换geom映射就好

# Args:
#   binwidth:宽度,类似Bar设置
#   ..count..:进行頻数统计,对应..density..是转换成概率统计
#   position='dodge' : 不进行堆叠,对比一下有无此参数就知道其作用
# -------------------------------------------- # 
# 直接elo的頻数分布图
binwidth <- 10 # 设置宽度
ggplot() + geom_line(aes(x = plotdata$elo, y = ..count..), 
                     position = 'dodge' ,stat="bin", binwidth=binwidth) +
  labs(x = "Elo") + 
  theme_bw()

# 按天画elo频数分布图
ggplot() + 
  geom_line(aes(x = plotdata$elo, y = ..count..,       
                col=as.factor(plotdata$Day)), 
            position = 'dodge' ,stat="bin", binwidth=binwidth, size=1) +
  scale_color_hue(name="") + 
  labs(x = "Elo") + 
  theme_hc()

# 按天画elo概率分布分布图,将..count..变..density...
ggplot() + 
  geom_line(aes(x = plotdata$elo, y = ..density..,       
                col=as.factor(plotdata$Day)), 
            position = 'dodge' ,stat="bin", binwidth=binwidth, size=1) +
  scale_color_hue(name="") + 
  labs(x = "Elo") + 
  theme_hc()

其结果对应如下:
这里写图片描述
这里写图片描述
这里写图片描述

其他

data.table 中提供了hour函数可以直接提取小时信息,xts包中有更加方便的提取小时,分钟等时间信息操作函数。直接研究不同小时下的分布。

# 数据处理
plot.data[, hour := hour(Time)]
plot.data
         elo     Day       Time     hour
     1:  344  2016-06-02  00:07:41    0
     2: 1285  2016-06-02  00:09:49    0
     3: 2006  2016-06-02  00:12:35    0
     4: 7355  2016-06-02  00:16:43    0
     5: 1820  2016-06-02  00:17:26    0
    ---                               
309206: 1522  2016-06-06  23:59:19   23
309207:  600  2016-06-06  23:59:12   23
309208:  981  2016-06-06  23:59:29   23
309209:  174  2016-06-06  23:59:40   23
309210:   76  2016-06-06  23:59:35   23 

按照上述画图函数画其改了分布图,顺便更改了一下图例位置结果如下:
这里写图片描述

画图代码如下:

ggplot() + geom_line(aes(x = plotdata$elo, y = ..density..,                           
                         col=as.factor(plotdata$hour)), 
                     position = 'dodge' ,stat="bin", binwidth=binwidth) +
           theme(legend.position =c(0.92,0.7), 
                 legend.text = element_text(size = 10, face = "bold")) +
           scale_color_hue(name="") + 
           labs(x = "Elo") +
           theme_bw()

总结

ggplot2可以快速的帮助我们对其分布进行观察,得益于binwidth的设置,它帮我们对数据进行了快速的离散化处理,而当在画图时很多参数都可以使用默认参数。若强迫症一定要好看一点,可以将其写成函数,方便多次调用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值