[R] two density curves in one plot?

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Wed Aug 23 13:25:58 CEST 2006


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" <niederlein-rstat at yahoo.de>
To: <R-help at 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 at 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



More information about the R-help mailing list