Reputation: 7517
I was wondering how I could find the height of the below histogram rectangles (i.e., counts
) for when values of x-axis are between -1 to +1 as shown by 10 BLUE points in the picture below?
set.seed(0)
x = rcauchy(5e4, 0, sqrt(2)/2)
cuts <- quantile(x, c(.025,.975))
cut.data = x[x>=cuts[1] & x<=cuts[2]]
h = hist(cut.data, breaks = 80)
axis(1, at = -9:9, font = 2)
Upvotes: 0
Views: 87
Reputation: 206232
You can draw those points with
with(h, {keep <- (mids>=-1 & mids<=1); points(mids[keep], counts[keep], col="blue", pch=19)})
so basically you get the center of the bars from h$mids
, check which values are in your desired range, and extract the corresponding count
h$counts[h$mids>=-1 & h$mids<=1]
Upvotes: 2
Reputation: 1473
The histogram object returned by the hist function has fields with both the histogram cell boundaries, the cell midpoints, and the counts within each cell. For example:
# get the counts of the mid points that are between -1 and 1
binIndices <- (h$mids > -1) & (h$mids < 1)
midVals <- h$mids[binIndices]
countVals <- h$counts[binIndices]
Upvotes: 0