chap 3.11 展示数据分布

直方图

  • 一维连续型分布:直方图
  • 注意调试binwidth/bins/breaks(组间距离/组数/切分位置)
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
  • 解决之道 : cut_width
ggplot(diamonds,aes(carat,depth))+
  geom_boxplot(aes(group=cut_width(carat,0.1)))+
  xlim(NA,2.05)
image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容