chap 3.11 展示数据分布

2021-02-18  本文已影响0人  陆慕熙

直方图

ggplot(diamonds,aes(depth))+
  geom_histogram()
image.png
#调整binwidth(组距宽度)/bins(组数)/breaks(切分位置)
ggplot(diamonds,aes(depth))+
  geom_histogram(binwidth = 0.1)+
  xlim(55,70)
image.png

如何比较不同组间数据分布的差异?

answer1:绘制多个小直方图[facet_wrap(~var)]
answer2: 绘制频数多边形并以颜色作为分类[geom_freqpoly()]
anser3:绘制条件密度图[geom_histogram(position="fill)]

分面多个小直方图

ggplot(diamonds,aes(depth))+
  geom_histogram(binwidth = 0.1)+
  facet_wrap(~cut)
image.png

频数多边形

#错误代码
ggplot(diamonds,aes(depth))+
  geom_histogram(binwidth = 0.1)+
  geom_freqpoly(aes(colour=cut))
image.png
#正确代码
ggplot(diamonds,aes(depth))+
  geom_freqpoly(aes(colour=cut),binwidth=0.1,na.rm = T)+
  xlim(58,68)+
  theme(legend.position = "none")
image.png

条件密度图

ggplot(diamonds,aes(depth))+
  geom_histogram(aes(fill=cut),binwidth=0.1,position = "fill",na.rm = T)+
  xlim(58,68)+
  theme(legend.position = "none")
image.png

直方图和频数多边形使用stat="bin"统计变换,此统计变换生成两个输出变量:count和density。默认将count作为y轴

  • density基本上等于各组频数除以总频数再乘以组距,此变量在需要比较不同分布的形状而非绝对大小时比较有用

密度估计geom_density():对每个数据点天上一点整他分布然后把所有曲线累加起来?

  • 仅在已知潜在的密度分布为平滑、连续且无界线的时候使用密度曲线图,可使用adjust参数调整所得密度曲线的平滑程度
ggplot(diamonds,aes(depth))+
  geom_density(na.rm = T)+
  xlim(58,86)+
  theme(legend.position = "none")
image.png
ggplot(diamonds,aes(depth))+
  geom_density(aes(fill=cut),na.rm = T)+
  xlim(58,86)+
  theme(legend.position = "none")
image.png
ggplot(diamonds,aes(depth))+
  geom_density(aes(fill=cut,colour=cut),na.rm = T)+
  xlim(58,86)+
  theme(legend.position = "none")
image.png
ggplot(diamonds,aes(depth,fill=cut,colour=cut))+
  geom_density(alpha=0.2,na.rm = T)+
  xlim(58,86)+
  theme(legend.position = "none")
image.png

每一条密度曲线下的面积都已经标准化为1,因此损失了有关个各子集间相对大小的信息

ggplot(diamonds,aes(carat,depth))+geom_boxplot()
image.png
ggplot(diamonds,aes(carat,depth))+
  geom_boxplot(aes(group=cut_width(carat,0.1)))+
  xlim(NA,2.05)
image.png
上一篇 下一篇

猜你喜欢

热点阅读