[R] Add function to histogram?

Jim Lemon bitwrit at ozemail.com.au
Wed Sep 21 21:27:36 CEST 2005

Robert Lundqvist wrote:
> Is there any neat way to add a curve (frequency function or the like) to a
> histogram or other plots? I haven't found one yet...
For a general solution, one usually has to scale the density function to 
fit the plot. The simple, but convenient, function rescale() in the 
plotrix package can be used like this:

add.density.curve<-function(y,density=NA) {
  if(is.na(density)) y.density<-density(y)

This only works for histograms, but I am working on a general function 
that will outsmart the habit of some functions of running the plot range 
into negative numbers when these are not possible (i.e. you can't have a 
negative count in a histogram - or a negative density except in the very 
latest physics).


More information about the R-help mailing list