0

I want to use ggplot2 to create a histogram with variable binwidths. I have integer values (JR) from 1 to 46 in different frequencies. I would like to create classes of 4 values each, i.e. 1-4, 5-8, etc.. The histogram should show 11 bars. The first 10 bars have the same width (4 values) and the last bar must be somewhat wider (6 values: 40-46). With geom_histogram I can only creatz a histogram with equal bars. This works fine with binwidth=2, but this is too small. Better is bindwith = 4. But then the histogram starts with 2 on x-axis. It would help, when the x-Axis starts with 1.

ggplot(data, aes(JR)) + geom_histogram(aes(), binwidth=4)
Waldi
  • 39,242
  • 6
  • 30
  • 78
Grit
  • 1
  • 1
    please have a look around.. https://rpubs.com/rodolfo_mendes/change-number-bins-histogram – Grzegorz Sapijaszko Feb 11 '22 at 19:17
  • 1
    or for a solution using ggplot: https://stackoverflow.com/questions/5948604/variable-width-bars-in-ggplot2-barplot-in-r – tivd Feb 11 '22 at 19:18
  • 2
    `geom_histogram` has a `breaks` argument. From the docs: "Alternatively, you can supply a numeric vector giving the bin boundaries." – stefan Feb 11 '22 at 19:23
  • Oh, thats so easy. Thank you a lot for your help. Problem solved :-) – Grit Feb 12 '22 at 13:38

0 Answers0