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))
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))
How can I get my bins to go from 1..20, 21..40, etc. for a larger dataset?
To change the number of bins in the histogram using the ggplot2 package library in the R Language, we use the bins argument of the geom_histogram() function. The bins argument of the geom_histogram() function to manually set the number of bars, cells, or bins the whole histogram will be divided into.
Specify Bins The default number of bins in ggplot2 is 30 . You can modify the number of bins using the bins argument. In the below example, we create a histogram with 7 bins.
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))
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With