Alpert, William
2005-Nov-03 16:45 UTC
[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@barrons.com [[alternative HTML version deleted]]
bogdan romocea
2005-Nov-03 17:00 UTC
[R] newbie graphics question: Two density plots in same frame ?
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 >
Francisco J. Zagmutt
2005-Nov-03 17:15 UTC
[R] newbie graphics question: Two density plots in same frame ?
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" <William.Alpert at barrons.com> >To: <r-help at 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 > >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
Deepayan Sarkar
2005-Nov-03 19:28 UTC
[R] newbie graphics question: Two density plots in same frame ?
On 11/3/05, Alpert, William <William.Alpert at barrons.com> 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
Reasonably Related Threads
- Intern at Barron's ! -- use statistics for investigative financial journalism
- Internship Opportunity at New York financial weekly
- Repeated measures 2-way anova -- robustness question
- Important note for AGI with PHP newbies
- freenode #asterisk IRC channel identd problem