0

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?

Histogram

Chris
  • 6,302
  • 1
  • 27
  • 54
NightDog
  • 91
  • 7

1 Answers1

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