Skip to content

superimpose density line over hist

8 messages · Albert Vilella, Dimitris Rizopoulos, Romain Francois +2 more

#
Hi all,

I'm trying to superimpose a rchisq density line over a histogram with
something like:

hist(alnlength)
lines(density(rchisq(length(alnlength), 4)),col="red")

But the rchisq line won't appear anywhere,

Anyone knows what I am missing here?

Thanks in advance,

    Albert.
#
try

hist(alnlength, prob = TRUE)

moreover I think that you do not need to sample from the Chi-squared 
to draw its density; you could use

dchisq(seq(...), 4))

instead.

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm



----- Original Message ----- 
From: "Albert Vilella" <avilella at gmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Tuesday, December 13, 2005 2:23 PM
Subject: [R] superimpose density line over hist
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
#
Le 13.12.2005 14:23, Albert Vilella a ??crit :
Hi Albert,

A few comments :
- your code should be reproductible, otherwise it is useless. (that 
recommandation is on the posting guide)

- that question is a top ten question on that list, go to the archive 
and you will find answers. (also posting guide)
BTW, it should be a FAQ and what about an example of overlaying in hist 
help page ?

- then if you want to superimpose an histogram and a density curve, they 
should be on the same scale, it is not the case since you missed the 
argument freq in your hist call, it should be :

R> hist(alnlength, freq=FALSE)

- Why do you simulate points to draw the density line ? Give a shot at :

R> curve(dchisq, df=4, col="red")


- That might interrest you :
http://addictedtor.free.fr/graphiques/search.php?q=hist

Romain
#
On 13-Dec-05 Albert Vilella wrote:
Well, it's certainly somewhere, but not within the window of your
graphic!

What you're missing is

a) Breaks in 'hist'

b) Allowing for (1) the widths of the bins, (2) the size of
   the sample, when you drawn the curve.

Example (this should work most of the time, but if it doesn't
because you have a sample that goes out of range just try again):

  alnlength<-rchisq(1000,4)
  x<-0.25*(0:100)
  hist(alnlength,breaks=0.25*(0:100))
  lines(x,1000*dchisq(x,4)*0.25)

Note that to get the 'lines' matching the histogram you have to
a) multiply the density by the binswidth to get probabilities;
b) multiply by the sample size so that the vertical scale of
   the curve matches that of the histogram (which here is showing
   counts).

If you don't want to set the breakpoints explicitly but leave it
to 'hist', you can subsequently extract the breakpoints from the
'hist' object and carry on from there.

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 13-Dec-05                                       Time: 15:50:21
------------------------------ XFMail ------------------------------
#
On 13-Dec-05 Albert Vilella wrote:
Following up my earlier reply, and more in line with your precise
question above:

  alnlength<-rchisq(1000,4)
  hist(alnlength,breaks=0.25*(0:100))
  j<-density(rchisq(1000,4))
  lines(j$x,1000*0.25*j$y)

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 13-Dec-05                                       Time: 15:59:50
------------------------------ XFMail ------------------------------
#
This certainly did the trick,

Thanks Ted, Sean, Romain, Dimitris, Lian and Andy,
And apologies for my "newbieness" in the posting,

    Albert.
#
........

    Romain> A few comments :
    Romain> - your code should be reproductible, otherwise it is useless. (that 
    Romain> recommandation is on the posting guide)

    Romain> - that question is a top ten question on that list, go to the archive 
    Romain> and you will find answers. (also posting guide)
    Romain> BTW, it should be a FAQ and what about an example of overlaying in hist 
    Romain> help page ?

What about the following one --- do also note the comments though!

set.seed(14)
x <- rchisq(100, df = 4)

## Comparing data with a model distribution should be done with qqplot()!
qqplot(x, qchisq(ppoints(x), df = 4)); abline(0,1, col = 2, lty = 2)

## if you really insist on using hist() ... :
hist(x, prob = TRUE, ylim = c(0, 0.2))
curve(dchisq(x, df = 4), col = 2, lty = 2, lwd = 2, add = TRUE)



    Romain> - then if you want to superimpose an histogram and a density curve, they 
    Romain> should be on the same scale, it is not the case since you missed the 
    Romain> argument freq in your hist call, it should be :

    R> hist(alnlength, freq=FALSE)

    Romain> - Why do you simulate points to draw the density line ? Give a shot at :

    R> curve(dchisq, df=4, col="red")

    ...........

Martin Maechler, ETH Zurich
#
Le 14.12.2005 11:54, Martin Maechler a ??crit :
Great !
I agree with you about qqplot, but a lot of people like that kind of 
hist-density overlay ...