PDF 112 / 520 into just the diamonds with a size of less than three carats and choose a smaller binwidth:
English · PDF 112
Original PDF page 112
中文 · PDF 112

筛选出仅包含小于三克拉的钻石,并选择更小的分箱宽度:

smaller <- diamonds %>% filter(carat < 3) ggplot(data = smaller, mapping = aes(x = carat)) + geom_histogram(binwidth = 0.1)

如果你想在同一张图中叠加多个直方图,我建议使用 geom_freqpoly() 而不是 geom_histogram()。geom_freqpoly() 执行与 geom_histogram() 相同的计算,但不是用条形显示计数,而是使用折线。重叠的折线比重叠的条形更容易理解:

ggplot(data = smaller, mapping = aes(x = carat, color = cut)) + geom_freqpoly(binwidth = 0.1)

这种类型的图存在一些挑战,我们将在第 93 页的“一个分类变量与一个连续变量”一节中再回来讨论。