João Pedro Pires
João Pedro Pires

Reputation: 53

Histogram in class interval in R

I need to plot an histogram, but my data are in an class interval.

the following are my inferior limit and superior.

xinf=c(0,10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5)
xsup=c(10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5,30)

frequency:

Fo=c(2,5,16,42,69,51,32,23,9,1)

I don't know how to plot an histogram with this data.

Upvotes: 0

Views: 4268

Answers (3)

r2evans
r2evans

Reputation: 160407

Because your data is already summarized, I won't be making a "proper" histogram. Instead, we can fake it with a bar plot:

barplot(Fo, width = xsup-xinf)

enter image description here

One of the nuances is that since it is a bar plot, there are spaces between the bars. This can be removed with:

barplot(Fo, width = xsup-xinf, space = 0)

enter image description here

or some other near-zero value.

Other optional components: axis(1), barplot(..., main="My Bars").

enter image description here

As @EdwardCarney started to suggest, you can refine the axis like this:

barplot(Fo, width = xsup-xinf, space = 0, main = "My Bars")
lbls <- sort(union(xinf, xsup))
axis(1, labels = lbls, at = lbls, las = 2)

enter image description here

(His suggestion was leaning towards centers of the bins, here I opted for the boundaries of them. Either way, you have options. Note that if you do not set space=0 then you have to adjust the locations of the labels, since everything will be spaced out.)

Upvotes: 2

Vandenman
Vandenman

Reputation: 3176

Here's one way:

# data
Fo=c(2,5,16,42,69,51,32,23,9,1)
xinf=c(0,10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5)

# breaks for histogram bars
breaks = c(xinf, 30)

# make a histogram with temp data
h = hist(rep(1, length(Fo)), breaks = xx, plot = FALSE)
# Fill the density and counts manually 
h$density = Fo / sum(Fo) 
h$counts = Fo
plot(h, freq = TRUE)

Do note that the freq = TRUE part is necessary to get counts on the y-axis. It also gives a warning message about the areas. Also, for the density estimation you could use something more sophisticated than what I used.

Upvotes: 0

marc1s
marc1s

Reputation: 779

Try something like

hist(rep((xinf+xsup)/2, Fo), breaks = union(xinf,xsup), 
     xlab = 'Variable name', main='Histogram')

Maybe I would change variables xinf and xsup by

xinf=c(9.5,10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5)
xsup=c(10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5,19.5)

Upvotes: 0

Related Questions