各位高手:本人正在阅读《ggplot2:数据分析与图形艺术》中文版一书,书中“5.4节 展示数据分布”(P72)以ggplot2包自带的数据集diamonds中的depth变量为例说明如何绘制频率多边图(frequency polygon),其代码如下:
> depth_dist <- ggplot(diamonds, aes(depth)) + xlim(58, 68)
> depth_dist + geom_freqpoly(aes(y = ..density.., colour = cut), binwidth = 0.1)
绘图结果如下:
试着举一反三,以diamonds数据集中的carat变量替代depth, 且相应修改取值范围为xlim(0, 3),其余不变。则修改后的代码如下:
> carat_dist <- ggplot(diamonds, aes(carat)) + xlim(0, 3)
> carata_dist + geom_freqpoly(aes(y = ..density.., colour = cut), binwidth = 0.1)
绘图结果如下:
比较两张图会发现:depth变量那张频率多边图的纵坐标刻度是[0, 1],表示频率,这个没啥问题;但carat变量的频率多边图的纵坐标刻度却不在[0, 1]范围内,而是显示为刻度从0到2以上,这似乎不是表示频率的刻度。数次验证甚至重启R也是如此。
请教高手,为什么会出现这种现象,如何解决?谢谢。