Hi, I wanted to plot two different density profiles. My code looks like: x1 <- rnorm(100,mean=0,sd=1) x2 <- rnorm(100,mean=1,sd=1) plot(density(x1),xlab="",col="red",main="") par(new=T) plot(density(x2),xlab="",col="blue",main="") However, the x-axis values don't match up for the two plots. Is there a way I can fix the axis so that the two plots are comparable? Similarly for the y-axis... thanks! [[alternative HTML version deleted]]
Try reading the docs! ?plot.default ... and note the xllim and ylim arguments. Have you read "An Introduction to R" or some other R tutorial. If not, you should do so before posting further. Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." Clifford Stoll On Wed, Jun 18, 2014 at 7:48 AM, Brian Smith <bsmith030465 at gmail.com> wrote:> Hi, > > I wanted to plot two different density profiles. My code looks like: > > > x1 <- rnorm(100,mean=0,sd=1) > x2 <- rnorm(100,mean=1,sd=1) > > plot(density(x1),xlab="",col="red",main="") > par(new=T) > plot(density(x2),xlab="",col="blue",main="") > > > However, the x-axis values don't match up for the two plots. Is there a way > I can fix the axis so that the two plots are comparable? Similarly for the > y-axis... > > thanks! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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.
First you should do your best to forget that you ever saw the command "par(new=T)", rip that page out of any book you saw it in, blacklist any webpage etc. In general (as you see) it causes more problems than it solves. Now to do what you want there are a couple of options, one of the simplest is to use the lines function: d1 <- density(x1) d2 <- density(x2) plot(d1, xlab='', col='red',main='', ylim=range(d1$y,d2$y), xlim=range(d1$x,d2$x) ) lines(d2, col='blue') Other options include the matplot function or tools in the lattice and ggplot2 packages. On Wed, Jun 18, 2014 at 8:48 AM, Brian Smith <bsmith030465 at gmail.com> wrote:> Hi, > > I wanted to plot two different density profiles. My code looks like: > > > x1 <- rnorm(100,mean=0,sd=1) > x2 <- rnorm(100,mean=1,sd=1) > > plot(density(x1),xlab="",col="red",main="") > par(new=T) > plot(density(x2),xlab="",col="blue",main="") > > > However, the x-axis values don't match up for the two plots. Is there a way > I can fix the axis so that the two plots are comparable? Similarly for the > y-axis... > > thanks! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com