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
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
Thank you both very much. It works!
With lattice graphics: library(lattice) d1 <- rnorm(100) d2 <- runif(100) densityplot(~ d1 + d2, auto.key = TRUE) On 8/23/06, Antje <niederlein-rstat at yahoo.de> 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 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. >
Hi All, I want to plot y~ x under the condition of variable a and b. Followed is the dataset: plotid lnden lnvol source 369 9037.0 10.419002 -4.101039226 S 370 9037.0 9.840548 -2.432385723 S 371 9037.0 8.973351 -1.374842169 S 372 9037.0 8.242756 -0.813800113 S 373 9037.0 8.006368 -0.366743413 S 374 9037.0 7.396335 -0.041375532 S 375 9037.0 6.194405 0.744573249 S 376 9038.0 10.417209 -2.938129138 S 377 9038.0 9.709296 -1.906228589 S 378 9038.0 8.581107 -1.187441385 S 379 9038.0 7.539027 -0.748873856 S 380 9038.0 6.866933 -0.228547521 S 381 9038.0 6.672033 0.222818889 S 382 9038.0 6.380123 0.863026089 S 1100 3.1 7.281089 5.563470357 P 2100 3.1 7.165854 5.587837467 P 3100 3.1 7.126938 5.604757978 P 4100 3.1 6.833951 5.709078555 P 560 3.1 6.634462 5.678818058 P 610 3.2 7.052830 5.534234273 P 710 3.2 6.905777 5.559511276 P 810 3.2 6.885776 5.590614404 P 910 3.2 6.685106 5.716040812 P 1010 3.2 6.495349 5.631784504 P 1110 3.3 6.697376 5.414815010 P 1210 3.3 6.553336 5.441823472 P 1310 3.3 6.581116 5.455788329 P 1410 3.3 6.279641 5.543868038 P 1510 3.3 6.119298 5.528003301 P 1610 3.4 7.035589 5.783924732 P 1710 3.4 6.875624 5.798852319 P 1810 3.4 6.812445 5.807787244 P I used par.plot(lnvol~lnden|source,data=dat,sub=as.factor(plotid),col=T); It gave good plots, but it put the different data sources to separated graphs, i.e. S and P. What I want is to plot them on the same graph. If anyone has the experience in doing plotting like this, please kindly give me some hints. Thanks! Jen. [[alternative HTML version deleted]]
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 <ggrothendieck at gmail.com> wrote:> Not sure who maintains the FAQ but its not me. > > > On 8/27/06, Ritwik Sinha <ritwik.sinha at gmail.com> 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 <ggrothendieck at gmail.com> wrote: > > > > > With lattice graphics: > > > > library(lattice) > > d1 <- rnorm(100) > > d2 <- runif(100) > > densityplot(~ d1 + d2, auto.key = TRUE) > > > > On 8/23/06, Antje <niederlein-rstat at yahoo.de > 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 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. > > > > > > > ______________________________________________ > > > > 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. > > > > > > > > > > -- > > 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