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

bogdan romocea br44114 at gmail.com
Thu Nov 3 18:00:33 CET 2005


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:William.Alpert at barrons.com]
> Sent: Thursday, November 03, 2005 11:45 AM
> To: r-help at 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
>
> william.alpert at barrons.com
>
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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
>




More information about the R-help mailing list