Search code examples
rggplot2histogram

R: what are the bands of the bins in when using a geom_histogram in package ggplot2?


I have created a histogram plot using the following code:

  p<-ggplot(df, aes(x=value)) +
    geom_histogram(color="black", fill="white", binwidth=5)
  
  p

enter image description here

My data ranges between 0 and 17. What are the bands of the bins e.g. is the 0 section 0-4? Is there a way of altering where the bands begin?


Solution

  • You can tell ggplot where you want one of the bins to be centered. For example, if you want bins between 0, 5, 10, etc you would do:

    ggplot(df, aes(x = value)) +
      geom_histogram(color = "black", fill = "white", binwidth = 5, center = 2.5) 
    


    Data

    set.seed(1)
    df <- data.frame(value = rpois(25000, 7))