I know how to draw histograms or other frequency/percentage related tables. But now I want to know, how can I get those frequency values in a table to use after the fact.
I have a massive dataset, now I draw a histogram with a set binwidth. I want to extract the frequency value (i.e. value on y-axis) that corresponds to each binwidth and save it somewhere.
Can someone please help me with this? Thank you!
Just in case someone hits this question with
ggplot
'sgeom_histogram
in mind, note that there is a way to extract the data from a ggplot object.The following convenience function outputs a dataframe with the lower limit of each bin (
xmin
), the upper limit of each bin (xmax
), the mid-point of each bin (x
), as well as the frequency value (y
).Illustration:
A related question I answered here (Cumulative histogram with ggplot2).
The
hist
function has a return value (an object of classhistogram
):From
?hist
: Valuean object of class "histogram" which is a list with components:
breaks
anddensity
provide just about all you need: