Set number of bins for histogram directly in ggplot

后端 未结 1 959
谎友^
谎友^ 2021-02-07 09:06

I\'d like to feed geom_histogram the number of bins for my histogram instead of controlling bins through binwidth. The documentation says I can do this

1条回答
  •  一生所求
    2021-02-07 09:32

    Just pass bins=x directly

    library(ggplot2)
    df <- data.frame(a = rnorm(10000))
    
    ggplot(df, aes(x=a)) + geom_histogram()
    

    Produces this (with warning "stat_bin() using bins = 30. Pick better value with binwidth."):

    And this:

    ggplot(df, aes(x=a)) + geom_histogram(bins=10)
    

    Produces:

    Using ggplot2 version 2.0.0

    0 讨论(0)
提交回复
热议问题