I used ggplot2 to plot a histogram shown below. I set the width of the bin into 5 or 10. However, the first bin in the chart seems to be only half of the bin width. 5-2.5 and 10-5. What caused this problem and how can I solve it?
Asked
Active
Viewed 483 times
1 Answers
0
You can control how bins line up with the center
and boundary
parameters. In this case, you probably want boundary = 0
library(ggplot2)
df <- data.frame(x = rnorm(100)*10)
ggplot(df, aes(x)) +
geom_histogram(binwidth = 5, boundary = 0, color = "black", fill = "red")

Elio Campitelli
- 1,408
- 1
- 10
- 20
-
it works! thank you! – NightDog Nov 30 '17 at 20:10