Hi, I am trying to plot two distribution graph on one plot. But I dont know how. I set them to the same x, y limit, even same x, y labels. Code: x1=rnorm(25, mean=0, sd=1) y1=dnorm(x1, mean=0, sd=1) x2=rnorm(25, mean=0, sd=1) y2=dnorm(x2, mean=0, sd=1) plot(x1, y1, type='p', xlim=range(x1,x2), ylim=range(y1, y2), xlab='x', ylab='y') plot(x2, y2, type='p', col="red", xlab='x', ylab='y') They just dont show up in one plot. Any hint will be very helpful. Thanks, Yun
par(mfrow=c(2,1)) #your plot #after plot par(mfrow=c(1,1)) On 23/02/07, Yun Zhang <yunzhang@princeton.edu> wrote:> > Hi, > > I am trying to plot two distribution graph on one plot. But I dont know > how. I set them to the same x, y limit, even same x, y labels. > > Code: > x1=rnorm(25, mean=0, sd=1) > y1=dnorm(x1, mean=0, sd=1) > > x2=rnorm(25, mean=0, sd=1) > y2=dnorm(x2, mean=0, sd=1) > plot(x1, y1, type='p', xlim=range(x1,x2), ylim=range(y1, y2), xlab='x', > ylab='y') > plot(x2, y2, type='p', col="red", xlab='x', ylab='y') > > They just dont show up in one plot. > > Any hint will be very helpful. > > Thanks, > Yun > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-ParanĂ¡ Brasil [[alternative HTML version deleted]]
Thanks. Now R plots two graphs on one plot. Yet they are still on two graphs, vertically parallelized with each other. But what I want to do is actually plotting two distribution on one single graph, using the same x and y axis. Like: | | | (dist2) | (dist 1) | ---------------------------> Is it possible to do that? Thanks, Yun Henrique Dallazuanna wrote:> par(mfrow=c(2,1)) > #your plot > #after plot > par(mfrow=c(1,1)) > > On 23/02/07, *Yun Zhang* <yunzhang at princeton.edu > <mailto:yunzhang at princeton.edu>> wrote: > > Hi, > > I am trying to plot two distribution graph on one plot. But I dont > know > how. I set them to the same x, y limit, even same x, y labels. > > Code: > x1=rnorm(25, mean=0, sd=1) > y1=dnorm(x1, mean=0, sd=1) > > x2=rnorm(25, mean=0, sd=1) > y2=dnorm(x2, mean=0, sd=1) > plot(x1, y1, type='p', xlim=range(x1,x2), ylim=range(y1, y2), > xlab='x', > ylab='y') > plot(x2, y2, type='p', col="red", xlab='x', ylab='y') > > They just dont show up in one plot. > > Any hint will be very helpful. > > Thanks, > Yun > > ______________________________________________ > R-help at stat.math.ethz.ch <mailto:R-help at stat.math.ethz.ch> mailing > list > https://stat.ethz.ch/mailman/listinfo/r-help > <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. > > > > > -- > Henrique Dallazuanna > Curitiba-Paran? > Brasil
Hi Yun, If you're asking how to place new graphic material on the same plot (e.g., several lines/points/etc in a single x-y region), this is covered in the Intro to R manual. E.g., you can do: plot(x1, y1, type='p', xlim=range(x1,x2), ylim=range(y1, y2), xlab='x', ylab='y') points(x2, y2, col="red") see ?lines ?points ?text ?abline Also, see the "new" option in par Best, Matt On 2/23/07, Yun Zhang <yunzhang at princeton.edu> wrote:> Hi, > > I am trying to plot two distribution graph on one plot. But I dont know > how. I set them to the same x, y limit, even same x, y labels. > > Code: > x1=rnorm(25, mean=0, sd=1) > y1=dnorm(x1, mean=0, sd=1) > > x2=rnorm(25, mean=0, sd=1) > y2=dnorm(x2, mean=0, sd=1) > plot(x1, y1, type='p', xlim=range(x1,x2), ylim=range(y1, y2), xlab='x', > ylab='y') > plot(x2, y2, type='p', col="red", xlab='x', ylab='y') > > They just dont show up in one plot. > > Any hint will be very helpful. > > Thanks, > Yun > > ______________________________________________ > 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. >-- Matthew C Keller Postdoctoral Fellow Virginia Institute for Psychiatric and Behavioral Genetics
Hi Yun, try this. x1 <- rnorm(10000, 0.5,0.4018) x2 <- rnorm(10000, 0.01919,0.3969) d1 <- density(x1) d2 <- density(x2) plot(range(d1$x,d2$x), range(d1$y, d2$y), type "n", xlab = "X", ylab = "Y" ) lines(d1, col = "blue",lwd=2) lines(d2, col = "red",lwd=2) Cheers Nguyen -----Original Message-----> From: r-help-bounces@stat.math.ethz.ch on behalfof Yun Zhang> Sent: Fri 2/23/2007 7:34 AM > To: Henrique Dallazuanna > Cc: r-help@stat.math.ethz.ch > Subject: Re: [R] How to plot two graphs on onesingle plot?> > Thanks. Now R plots two graphs on one plot. > Yet they are still on two graphs, verticallyparallelized with each other.> > But what I want to do is actually plotting twodistribution on one> single graph, using the same x and y axis. Like: > | > | > | (dist2) > | (dist 1) > | > ---------------------------> > > Is it possible to do that? > > Thanks, > Yun > > Henrique Dallazuanna wrote: > > par(mfrow=c(2,1)) > > #your plot > > #after plot > > par(mfrow=c(1,1)) > > > > On 23/02/07, *Yun Zhang* <yunzhang@princeton.edu > > <mailto:yunzhang@princeton.edu>> wrote: > > > > Hi, > > > > I am trying to plot two distribution graphon one plot. But I dont> > know > > how. I set them to the same x, y limit, evensame x, y labels.> > > > Code: > > x1=rnorm(25, mean=0, sd=1) > > y1=dnorm(x1, mean=0, sd=1) > > > > x2=rnorm(25, mean=0, sd=1) > > y2=dnorm(x2, mean=0, sd=1) > > plot(x1, y1, type='p', xlim=range(x1,x2),ylim=range(y1, y2),> > xlab='x', > > ylab='y') > > plot(x2, y2, type='p', col="red", xlab='x',ylab='y')> > > > They just dont show up in one plot. > > > > Any hint will be very helpful. > > > > Thanks, > > Yun > >[[alternative HTML version deleted]]