Re: [R] two density curves in one plot?

2006-08-27 Thread Ritwik Sinha
Hi Gabor and Dimitris,

I was wondering if this question was frequent enough to be in the R
FAQ under R Miscellanea and thought of something like this

Q. How do I plot two curves on the same graph?

A. Plot the first curve using the plot() command and add lines using
lines(). For example

d1 - density(rnorm(100))
d2 - density(rnorm(100))
plot(range(d1$x, d2$x), range(d1$y, d2$y), type = n, xlab = x,
ylab = Density)
lines(d1, col = red)
lines(d2, col = blue)

Alternatively one can use points() to add points to the plot.

If you think this question should be in the FAQ and if you have any
comments/changes to the QA then I can request the maintainer of the
FAQ to include it. We could also include a lattice solution but I was
thinking of not complicating things.

Ritwik Sinha

On 8/27/06, Gabor Grothendieck [EMAIL PROTECTED] wrote:
 Not sure who maintains the FAQ but its not me.


 On 8/27/06, Ritwik Sinha [EMAIL PROTECTED] wrote:
  This seems to be a common question for new commers to R, does it make sense
  to add it to the R FAQ page? I checked it is not currently there.
 
  Ritwik
 
 
  On 8/23/06, Gabor Grothendieck [EMAIL PROTECTED] wrote:
  
  With lattice graphics:
 
  library(lattice)
  d1 - rnorm(100)
  d2 - runif(100)
  densityplot(~ d1 + d2, auto.key = TRUE)
 
  On 8/23/06, Antje [EMAIL PROTECTED]  wrote:
   Hello,
  
   I was wondering if I can plot two curves I get from density(data) into
   one plot. I want to compare both.
   With the following commad, I just get one curve plotted:
  
   plot( density(mydata) )
  
   Sorry for this stupid question but I could not find a solution until
  now...
  
   Antje
  
   __
   R-help@stat.math.ethz.ch mailing list
   https://stat.ethz.ch/mailman/listinfo/r-help
   PLEASE do read the posting guide
  http://www.R-project.org/posting-guide.html
   and provide commented, minimal, self-contained, reproducible code.
  
 
  __
 
  R-help@stat.math.ethz.ch mailing list
  https://stat.ethz.ch/mailman/listinfo/r-help
  PLEASE do read the posting guide
  http://www.R-project.org/posting-guide.html
  and provide commented, minimal, self-contained, reproducible code.
 
 
 
 
  --
  Ritwik Sinha
  Graduate Student
  Epidemiology and Biostatistics
  Case Western Reserve University
 
  http://darwin.cwru.edu/~rsinha




-- 
Ritwik Sinha
Graduate Student
Epidemiology and Biostatistics
Case Western Reserve University

http://darwin.cwru.edu/~rsinha

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] two density curves in one plot?

2006-08-23 Thread Antje
Hello,

I was wondering if I can plot two curves I get from density(data) into 
one plot. I want to compare both.
With the following commad, I just get one curve plotted:

plot( density(mydata) )

Sorry for this stupid question but I could not find a solution until now...

Antje

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] two density curves in one plot?

2006-08-23 Thread Dimitris Rizopoulos
try this:

x1 - rnorm(1000)
x2 - rnorm(1000)

d1 - density(x1)
d2 - density(x2)
plot(range(d1$x, d2$x), range(d1$y, d2$y), type = n,
 xlab = x, ylab = Density)
lines(d1, col = red)
lines(d2, col = blue)


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://med.kuleuven.be/biostat/
 http://www.student.kuleuven.be/~m0390867/dimitris.htm


- Original Message - 
From: Antje [EMAIL PROTECTED]
To: R-help@stat.math.ethz.ch
Sent: Wednesday, August 23, 2006 1:11 PM
Subject: [R] two density curves in one plot?


 Hello,

 I was wondering if I can plot two curves I get from density(data) 
 into
 one plot. I want to compare both.
 With the following commad, I just get one curve plotted:

 plot( density(mydata) )

 Sorry for this stupid question but I could not find a solution until 
 now...

 Antje

 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide 
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.
 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] two density curves in one plot?

2006-08-23 Thread Antje
Thank you both very much.
It works!

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] two density curves in one plot?

2006-08-23 Thread Gabor Grothendieck
With lattice graphics:

library(lattice)
d1 - rnorm(100)
d2 - runif(100)
densityplot(~ d1 + d2, auto.key = TRUE)

On 8/23/06, Antje [EMAIL PROTECTED] wrote:
 Hello,

 I was wondering if I can plot two curves I get from density(data) into
 one plot. I want to compare both.
 With the following commad, I just get one curve plotted:

 plot( density(mydata) )

 Sorry for this stupid question but I could not find a solution until now...

 Antje

 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.