R 为什么geom_直方图从负bin下限开始,即使所有值均为>;0?

R 为什么geom_直方图从负bin下限开始,即使所有值均为>;0?,r,ggplot2,R,Ggplot2,我正在试用H.Wickham的R书中的钻石数据集。 在钻石的默认geom_柱状图中,x=克拉,binwidth为0.5,但bin 1从-0.25开始,即使克拉的最低值为0.2。为什么会这样?为上下文附加pic和代码。谁能帮忙解释一下。谢谢 ##geom_histogram geom_histogram(mapping=aes(x = carat),binwidth = 0.5) summary(diamonds) ##dplyr to get count of cut[![enter imag

我正在试用H.Wickham的R书中的钻石数据集。 在钻石的默认geom_柱状图中,x=克拉,binwidth为0.5,但bin 1从-0.25开始,即使克拉的最低值为0.2。为什么会这样?为上下文附加pic和代码。谁能帮忙解释一下。谢谢

##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移动垃圾箱。可以使用文档中讨论的
边界
中心
参数执行此操作

库(ggplot2)
图书馆(拼凑)
##几何直方图

p1
cut_width
对宇宙的物理定律一无所知,所以不知道克拉应该是正的。让我们看看它为什么这么做。我目前使用的是
ggplot2-3.2.1
,因此某些行可能已在较新版本中更新

debugonce(剪切宽度)
切割宽度(钻石$克拉,0.5)
#调试:{

#x您可以使用
seq
手动定义直方图的中断

library(ggplot2)
library(dplyr)

data("diamonds")

brks <- unique(seq(0, ceiling(max(diamonds$carat)), by = 0.5))

diamonds %>%
  mutate(bin = cut_width(carat, width = 0.5, breaks = brks, boundary = TRUE)) %>%
  count(bin)
## A tibble: 10 x 2
#   bin       n
#   <fct> <int>
# 1 0     18932
# 2 0.5   17506
# 3 1     12060
# 4 1.5    3553
# 5 2      1763
# 6 2.5      94
# 7 3        23
# 8 3.5       4
# 9 4         4
#10 5         1

ggplot(diamonds) +
  geom_histogram(mapping=aes(x = carat), binwidth = 0.5, breaks = brks)
库(ggplot2)
图书馆(dplyr)
数据(“钻石”)
brks%
突变(料仓=切割宽度(克拉,宽度=0.5,断裂=brks,边界=TRUE))%>%
计数(箱)
##一个tibble:10x2
#宾n
#    
# 1 0     18932
# 2 0.5   17506
# 3 1     12060
# 4 1.5    3553
# 5 2      1763
# 6 2.5      94
# 7 3        23
# 8 3.5       4
# 9 4         4
#10 5         1
ggplot(钻石)+
几何柱状图(映射=aes(x=克拉),binwidth=0.5,breaks=BRK)
请参阅
帮助('pretty')
。然后运行
pretty(钻石$carat)
。最小值为
0
,它是一组振幅
0.5
的中心,间隔
[-0.25.0.25]