rhistogrambins

Creating a histogram in R knowing bin heights


I'm trying to make a histogram in R in a sort of backwards manner, where I already know how many bins I want, and how many observations are in each bin. My data looks like this

Interval 0-2 2-4 4-6 6-10 10-15 15-25 >25
Number of observations 6 9 7 9 6 7 5

I have the data saved in the format obs<-c(6,9,7,9,6,7,5). But trying to run hist(obs) of course creates a histogram which counts how many of the bins have between 5-6 observations, how many between 6-7, and so on, which is the opposite of what I want.

I tried using barplot, but it comes out looking wierd. Is there a way to use a hist-style plot, where I specifically get the bins

(-∞, 2], (2,4], (4,6], ..., (25,∞)?

with the respective heights 6, 9, 7, 6, 7, 5?


Solution

  • This is just a one-liner after having the data set read in.

    barplot(as.matrix(df1[-1]))
    

    enter image description here


    Data

    df1 <- read.table(text = "
    Interval    0-2     2-4     4-6     6-10    10-15   15-25   >25
    'Number of observations'    6   9   7   9   6   7   5
    ", header = TRUE, check.names = FALSE)