0

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?

  • 3
    What you are describing is a really just a bar chart, not a histogram. How is your data stored in R? It would be easier to help if you provided data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) such as a `dput()` to make it more clear. You seem to be using base R so try `barplot`. Maybe you can say more about what exactly is "weird" about the barplot and show how you are calling it. – MrFlick Jun 10 '21 at 16:57

2 Answers2

0

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)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
0

If you wanted to use ggplot, you could go for: geom_col()

e.g.

ggplot(data_table, aes(x=obseration_class, y=value_of_class)) + geom_col()
LC117
  • 426
  • 4
  • 12