我如何控制ggplot2中的bin间隔? [英] How can i control bin intervals in ggplot2?
问题描述
我无法正确控制垃圾箱是否来自例如当我说 binwidth = 20
时,从-10到+10或从0到20,我得到了前者,但是我有从1开始的数据,并且我不希望间隔变成负数.
I cant correctly control if a bin is going from e.g. -10 to +10 or from 0 to 20 when I say binwidth = 20
i get the former but I have data that begins at 1 and I dont want the interval to go into the negatives.
这是我的问题的一个例子:
Here is an example of my problem:
testData = data.frame(x=c(1,4,6,9,9))
ggplot(data=testData, aes(x=testData$x)) +
geom_histogram(binwidth=3, aes(col=I("white"))) +
scale_x_continuous(breaks=c(1,2,3,4,5,6,7,8,9,10))
足够奇怪,如果我使用 binwidth = 2
,我最终会遇到我想要的间隔:
strange enough, if I use binwidth = 2
I end up with intervals like I want:
ggplot(data=testData, aes(x=testData$x)) +
geom_histogram(binwidth=2, aes(col=I("white"))) +
scale_x_continuous(breaks=c(1,2,3,4,5,6,7,8,9,10))
对于更大的数据集,如何使我的垃圾箱从1..20、21..40等位置移动?
How can I get my bins to go from 1..20, 21..40, etc. for a larger dataset?
推荐答案
您可以使用 geom_histogram
的 center
自变量来做到这一点,如下所示:
You can do this by using the argument center
of geom_histogram
as follows:
# Make some random test data
testData = data.frame(x=runif(1000,min=1,max=110))
# Construct the plot
ggplot(data=testData, aes(x=testData$x)) +
geom_histogram(binwidth=20,
center = 11,
aes(col=I("white"))) +
scale_x_continuous(breaks=seq(1,max(testData$x) + 20, by = 20))
通过指定一个箱的宽度和中心,您可以定义该箱应为20宽,并以11为中心.因此,第一个箱将为1到21.
By specifying the binwidth and the center for one bin, you define that the bin should be 20 wide and be centered around 11. So the first bin will be 1 to 21.
我还添加了一个 seq()
调用,以构造x轴刻度,而无需手动键入所有刻度.结果图如下:
I also added a seq()
call to construct the x axis ticks without having to type all of them manually. The resulting plot is the following:
这篇关于我如何控制ggplot2中的bin间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!