[R] Need ideas on how to show spikes in my data and how to code it in R

S Ellison S.Ellison at lgc.co.uk
Tue Jul 8 13:45:11 CEST 2008

Two thoughts:
i) If you have a narrow distribution, the density can be higher than 1. The area comes out at 1 for density, and n for the frequency. 

ii) hist() will not show the same frequencies as density() unless hist has unit bin sizes. density*length is showing number per unit change in Weight; hist shows number per bin width.

Try plotting a histogram first, then plot the density on top of that. If they disagree >>> "Thomas Fröjd" <tfrojd at gmail.com> 07/08/08 12:29 PM >>>

Sorry for bothering you again but I can't seem to get it right.

When i multiply the density with the number of observations it seems
to be way to high, The reference curve is drawn at maybe 20 times
higher frequency count than it should be.

I use the following code where "weights$Weight" is my weights data and
"reference" is my reference dataset.

# calucate the right breakpoints
breakpoints <- seq(min(weights), max(weights), by=binwidth)

#scale density
dens <- density(reference)
dens$y <- dens$y * (length(weights$Weight))

#graph it
hist(weights$Weight, freq=TRUE, breaks=breakpoints, main=wfiles[i])


Any ideas are greatly appreciated.


On Fri, Jun 27, 2008 at 10:54 PM, Daniel Folkinshteyn
<dfolkins at gmail.com> wrote:
> if you want the "frequency" scale rather than density scale, then leave hist
> as is (by default it uses the frequency scale), and rescale the density by
> multiplying it by the appropriate NOBS.
> on 06/27/2008 01:16 PM Thomas Frööjd said the following:
>> Hi
>> Thank you very much for taking time to answer.
>> The solution of using hist(data) for the main dataset and adding
>> lines(density(refdata)) for the reference data seem to work great. I
>> forgot to mention one thing however, I need to have frequency on the y
>> azis instead of density as now.
>>  I know this is not a "real" histogram but since the audience is not
>> very statistically experienced I would prefer to do it this way.
>> Anyone have an idea?
>> Thanks again for your help.
>> Thomas Fröjd
>> On Wed, Jun 25, 2008 at 6:16 PM, Daniel Folkinshteyn <dfolkins at gmail.com>
>> wrote:
>>>>   I don't understand this.  Why not just get hist() to plot on the
>>>> density scale,
>>>>   thereby making its output commensurate with the output of density()?
>>>>   The hist() function will plot on the density scale if you ask it to.
>>>>  Set freq=FALSE
>>>>   (or prob=TRUE) in the call to hist.
>>> ehrm... because i didn't realize that option existed :) that certainly is
>>> easier than manually scaling hist output by NOBS - thanks for the tip!

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

This email and any attachments are confidential. Any use...{{dropped:8}}

More information about the R-help mailing list