[R] Histogram from frequency table?

Duncan Murdoch murdoch at stats.uwo.ca
Fri Jan 11 15:14:36 CET 2008


On 1/11/2008 8:55 AM, Brian Nguyen wrote:
> Hi, I've had some trouble figuring out how to produce a histogram in R directly given a frequency table or relative frequency table. I've looked through the documentation and mailing list, and have only found information on producing histograms given the original data set. Any help would be appreciated!
> 
> An example of what I'd like to do would be to take the following frequency table:
> 
> Class   Freq    Rel Freq
> =============
> [1,2)    1            1/3
> [2,3)    2            2/3
> 
> and translate it into the corresponding histogram.


The way hist() works is to create an object of class histogram, and then 
it plots it.  You could create the object directly.  For the example 
above, it would go like this:

myhist <- list(breaks = 1:3, counts = 1:2, density = (1:2)/3, xname="X")
class(myhist) <- "histogram"
plot(myhist)

If you know you want a freq=TRUE histogram, you can skip density; if you 
know you want a freq=FALSE histogram, you can skip counts.  You can see 
in the source to graphics:::plot.histogram that the "equidist" component 
of the histogram isn't really needed.

Duncan Murdoch




More information about the R-help mailing list