0

How can I make a histogram in RStudio with the frequency on the y-axis and on the x-axis pre-defined non-uniform intervals, for example: 0-50, 50-150, 150-500, 500-2000.

I have hypothetical data, as I would not like to share my original data.

data <- c(1,1.2,40,1000,36.66,400.55,100,99,2,1500,333.45,25,125.66,141,5,87,123.2,61,93,85,40,205,208.9)
Muon
  • 1,294
  • 1
  • 9
  • 31
wesleysc352
  • 579
  • 1
  • 8
  • 21

1 Answers1

1

Set the breaks you want with the breaks argument in the hist function.

hist(data, breaks=c(0, 50, 150, 500, 2000), freq=TRUE)

enter image description here

Update

If you want even bar widths you need to put your data into a categorical format rather than continuous. You could achieve this by doing the following.

# Cut your data into categories using your breaks
data <- cut(data, 
            breaks=c(0, 50, 150, 500, 2000),
            labels=c('0-50', '50-150', '150-500', '500-2000'))
# Make a data table (i.e. a frequency count)
data <- table(data)
# Plot with `barplot`
barplot(data)

enter image description here

Muon
  • 1,294
  • 1
  • 9
  • 31