Reputation: 1187
In Minitab, it is easy to create "cutpoint" histograms. Can this be done in ggplot2
?
For example,
df <- data.frame(x = c(0.08, 0.21, 0.25, 0.4, 0.5, 0.6))
ggplot(df, aes(x = x)) + geom_histogram(binwidth = 0.1)
As you can see here, R
defaults to "midpoint" histograms. The bar containing the 0.08
is being marked with a bar at the 0.1
. The bar containing the 0.21
and 0.25
is being marked at the 0.2
and so forth.
Can I somehow change these bars so the first bar covers the area between 0
and 0.1
, and the second bar covers the area between 0.2
and 0.3
, and so forth?
Upvotes: 0
Views: 1962
Reputation: 442
You can get rid of the problem in two ways: using parameter "center" or using "boundary". With "center" you can specify the center of one of the bins, "boundary" is similar but you specify the value of a boundary between two bins. Worth noting that "center" and "boundary" can be either above or below the range of the data, in this case the value provided will be shifted of an adequate number of widths.
In this case you already know the width of the bin, its boundaries, so with this parameters you can easily do what you asked:
library(ggplot2)
df <- data.frame(x = c(0.08, 0.21, 0.25, 0.4, 0.5, 0.6))
# This is "center" solution:
ggplot(df, aes(x = x)) + geom_histogram(binwidth = 0.1, center=0.05)
# This is with "boundary" parameter
ggplot(df, aes(x = x)) + geom_histogram(binwidth = 0.1, boundary=0.1)
You can find details and more information on the reference ?geom_histogram. Hope this helps
Upvotes: 1