17

This may sound a like a repeat question, but hopefully it is not. In the basic R graphics histogram function, we have a option breaks="FD", which gives a reasonable sized binsize for the histogram, do we have any similar simple option for ggplot2? Or even better can we use the same option in ggplot2?

I do understand that you can adjust the binwidth in geom_histogram, but i am looking for a more simpler way to generate aesthetically pleasing and resonable binsize.

Sam
  • 7,922
  • 16
  • 47
  • 62
  • 2
    Could you calculate it yourself using `nclass.FD` and then set `binwidth` accordingly...? – joran Jan 07 '13 at 16:48
  • 2
    I don't believe FD does give a reasonable sized binwidth in many cases (it's generally too large for big datasets) which is one of the reasons it's not built in. – hadley Jan 08 '13 at 20:16

1 Answers1

18
set.seed(42)
x <- rnorm(1000)
hist(x,breaks="FD")

library(ggplot2)
breaks <- pretty(range(x), n = nclass.FD(x), min.n = 1)
bwidth <- breaks[2]-breaks[1]
df <- data.frame(x)
ggplot(df,aes(x))+geom_histogram(binwidth=bwidth,fill="white",colour="black")
Roland
  • 127,288
  • 10
  • 191
  • 288