问题描述
我正在尝试H.Wickham在R book中的钻石数据集.在x =克拉的钻石的默认geom_histogram中,binwidth是0.5,但是bin 1从-0.25开始,即使克拉的最低值为0.2.为什么会这样呢?附加图片和上下文代码.谁能帮忙解释一下.谢谢.
I'm trying out the diamonds dataset in R book by H.Wickham. In the default geom_histogram for diamonds where x = carat, the binwidth is 0.5 but bin 1 starts at -0.25 even though the lowest value for carat is 0.2. Why would this be so? Attaching pic and code for context. Can anyone help explain. Thanks.
##geom_histogram
geom_histogram(mapping=aes(x = carat),binwidth = 0.5)
summary(diamonds)
##dplyr to get count of cut[![enter image description here][1]][1]
diamonds %>%
count(cut_width(carat,0.5))
推荐答案
有帮助吗?
在p1中,第一个容器位于0的中心.但是您希望容器的左侧从0-p2开始.因此,您必须告诉ggplot转移垃圾箱.您可以使用文档中讨论的 boundary
或 center
自变量来完成此操作.
In p1 the first bin is centered on 0. But you want the left hand side of the bin to start with 0 - p2. So you have to tell ggplot to shift the bins. You can do this using a boundary
or center
argument which are discussed in the documentation.
library(ggplot2)
library(patchwork)
##geom_histogram
p1 <-
ggplot(diamonds)+
geom_histogram(mapping=aes(x = carat), binwidth = 0.5)+
ggtitle("p1 bars centred on bin boundaries")
p2 <-
ggplot(diamonds)+
geom_histogram(mapping=aes(x = carat), binwidth = 0.5, boundary = 0)+
ggtitle("p2 bars between bin boundaries")
p1+p2
这篇关于为什么geom_histogram从负bin下限开始,即使所有值都大于>0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!