[R] Density Plots

Jason Turner jasont at indigoindustrial.co.nz
Mon Jan 5 02:05:47 CET 2004

David Tyler wrote (using an e-mail client that doesn't wrap lines):

> I am using an older version of R (1.6.2) to run a Monte Carlo 
> simulation, generating 10,000 samples per 'run'.  When I plot 
 > histograms I get the expected 'bins' on the x-axis and the
 > frequency distribution on the y-axis. However when I ask R
 > to plot the SAME data set with a density curve the x-axis
 > emains the same but the y-axis can generate values of up to 1e8 etc.

> Can anyone (a) explain why this might be so and/or (b) suggest a fix?


hist(..., freq=FALSE)

This should give the same numbers as the density plots' y-axes.

It sounds like you've got a narrow range of x-axis values (small 
numbers, or small differences between them, or both).  The total area 
under a density estimate curve must equal 1 by definition, so nothing's 
really "broken".  The only fix is to re-scale the x axis to different 
units, or draw a different y-axis on after the fact.  Something like...

foo <- density(...)
plot(foo, yaxt="n")
axis(...)  # something that means something to you here.

Since this isn't a density plot any longer, it would help to be clear to 
your readers what's going on with the plots.

Hope that helps


Indigo Industrial Controls Ltd.
jasont at indigoindustrial.co.nz

More information about the R-help mailing list