[R] newbie graphics question: Two density plots in same frame ?

2005-11-03 Thread Alpert, William
I swear I've scoured the help files and several texts before posting
what feels like a dumb newbie question.  
 
How can I draw two kernel density plots in the same frame ? I have
similar variables in two separate data frames, and I would like to show
their two histograms/densities in a single picture.  Same units, scale,
range for both, so I'm simply trying to draw one and then add the other
to the picture.  Nothin' fancy.
 

Bill Alpert

Sr. Editor

Barron's

212.416.2742

[EMAIL PROTECTED]

 

 

[[alternative HTML version deleted]]

__
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


Re: [R] newbie graphics question: Two density plots in same frame ?

2005-11-03 Thread bogdan romocea
Here's a function that you can customize to fit your needs. lst is a named list.

multicomp - function(lst)
{
clr - c(darkgreen,red,blue,brown,magenta)
alldens - lapply(lst,function(x) {density(x,from=min(x),to=max(x))})
allx - sapply(alldens,function(d) {d$x})
ally - sapply(alldens,function(d) {d$y})
plot(allx,ally,type=n)
for (i in 1:length(lst)) {
lines(alldens[[i]]$x,alldens[[i]]$y,lty=i,col=clr[i],lwd=3)
}
legend(topright,xjust=1,legend=names(lst),lwd=3,lty=1:length(lst),col=head(clr,length(lst)))
}
#---
toplot - list(var1=dfr1$var,var2=dfr2$var)
multicomp(toplot)


 -Original Message-
 From: Alpert, William [mailto:[EMAIL PROTECTED]
 Sent: Thursday, November 03, 2005 11:45 AM
 To: r-help@stat.math.ethz.ch
 Subject: [R] newbie graphics question: Two density plots in
 same frame ?


 I swear I've scoured the help files and several texts before posting
 what feels like a dumb newbie question.

 How can I draw two kernel density plots in the same frame ? I have
 similar variables in two separate data frames, and I would
 like to show
 their two histograms/densities in a single picture.  Same
 units, scale,
 range for both, so I'm simply trying to draw one and then add
 the other
 to the picture.  Nothin' fancy.


 Bill Alpert

 Sr. Editor

 Barron's

 212.416.2742

 [EMAIL PROTECTED]





   [[alternative HTML version deleted]]

 __
 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


__
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


Re: [R] newbie graphics question: Two density plots in same frame ?

2005-11-03 Thread Francisco J. Zagmutt
To plot two Kernel densities you can use matplot:

x1-density(rnorm(100))
x2-density(rnorm(100))
matplot(cbind(x1$y,x2$y), type=l)


Or if both distributions are really very similar and you don't have to 
adjust the axes you can simply use
plot(x1)
lines(x2, col=red)


Finally if you want to have two histograms in the same picture (I would not 
recomend it tough since the distributions are similar so the overlapping 
will make it very messy) you can use the argument add within hist

hist(rnorm(100), col=red)
hist(rnorm(100), col=blue, add=T)

I hope this helps

Francisco

From: Alpert, William [EMAIL PROTECTED]
To: r-help@stat.math.ethz.ch
Subject: [R] newbie graphics question: Two density plots in same frame ?
Date: Thu, 3 Nov 2005 11:45:21 -0500

I swear I've scoured the help files and several texts before posting
what feels like a dumb newbie question.

How can I draw two kernel density plots in the same frame ? I have
similar variables in two separate data frames, and I would like to show
their two histograms/densities in a single picture.  Same units, scale,
range for both, so I'm simply trying to draw one and then add the other
to the picture.  Nothin' fancy.


Bill Alpert

Sr. Editor

Barron's

212.416.2742

[EMAIL PROTECTED]





   [[alternative HTML version deleted]]

__
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

__
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


Re: [R] newbie graphics question: Two density plots in same frame ?

2005-11-03 Thread Deepayan Sarkar
On 11/3/05, Alpert, William [EMAIL PROTECTED] wrote:
 I swear I've scoured the help files and several texts before posting
 what feels like a dumb newbie question.

 How can I draw two kernel density plots in the same frame ? I have
 similar variables in two separate data frames, and I would like to show
 their two histograms/densities in a single picture.  Same units, scale,
 range for both, so I'm simply trying to draw one and then add the other
 to the picture.  Nothin' fancy.

Using densityplot from lattice:

library(lattice)
d1 = data.frame(x = rnorm(100))
d2 = data.frame(x = rnorm(100, mean = 0.5))
densityplot(~d1$x + d2$x, plot.points = FALSE)

-Deepayan

__
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