Dear R users, I need to compare two scatter plots, plot(x1, y1) plot(x2, y2) and would like to plot them in the same figure. How do I do it? Thank you. -- View this message in context: http://www.nabble.com/two-scatter-plots-in-one-tp21963375p21963375.html Sent from the R help mailing list archive at Nabble.com.
Set the xlim/ylim to the extent of the data and then do plot(x1, y1, xlim=range(x1, x2), ylim=range(y1, y2)) points(x2, y2, col='red') On Wed, Feb 11, 2009 at 3:43 PM, liujb <liujulia7 at yahoo.com> wrote:> > Dear R users, > > I need to compare two scatter plots, > plot(x1, y1) > plot(x2, y2) > > and would like to plot them in the same figure. How do I do it? > > Thank you. > > -- > View this message in context: http://www.nabble.com/two-scatter-plots-in-one-tp21963375p21963375.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
liujb wrote:> Dear R users, > > I need to compare two scatter plots, > plot(x1, y1) > plot(x2, y2) > > and would like to plot them in the same figure. How do I do it? > >Hi liujb, How about this: plot(x1,y1,xlim=range(c(x1,x2)),ylim=range(c(y1,y2)),col="red") points(x2,y2,col="blue") Jim
lattice and ggplot2 also offer a general way of doing this,> # first create a data.frame in the long format containing the two > data sets > x1 <- seq(-10, 10) > x2 <- seq(-8, 12) > y1 <- sin(x1/3) > y2 <- cos(x2/2) > > d1 <- data.frame(x=x1, y=y1, var="1") > d2 <- data.frame(x=x2, y=y2, var="2") > > library(reshape) > d <- melt(merge(d1, d2, all=T), id=c("x", "var")) > > # here goes the plotting in a high-level perspective > > library(ggplot2) > > qplot(x, value, data=d, geom=c("line","point"), colour=var) > > or, > > library(lattice) > > xyplot(value ~ x, data=d, type="b", groups = var)On 12 Feb 2009, at 09:09, Jim Lemon wrote:> liujb wrote: >> Dear R users, >> >> I need to compare two scatter plots, >> plot(x1, y1) >> plot(x2, y2) >> >> and would like to plot them in the same figure. How do I do it? >> >> > Hi liujb, > How about this: > > plot(x1,y1,xlim=range(c(x1,x2)),ylim=range(c(y1,y2)),col="red") > points(x2,y2,col="blue") > > > Jim > > ______________________________________________ > 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._____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag