Hi, R-listers I tried to plot several graphs in a sigle x-y coordinate settings, like the following: |(y) s | ****** s | ***** s | sssssssssssssssssss |_______________________________(x) where "*" and "s" denote two diffrent plots. However, when I used plot(data1); % data1 is the data points of "*" par(new=T); plot(data2); % data2 is the data points of "s" I found that the x and y labels are messed up, since different graphs use diffrent unit length on the x-axis and y-axis. So is there someway to avoid this problem? Or is there some other function plottting multiple plots in one x-y axis setting? Thanks for your point and help. Fred
Jerome Asselin
2003-Apr-10 19:59 UTC
[R] How to plot several graphs in a single 2-D figure?
This has been discussed previously. You can get the idea from the R Help archive. See the thread on "multiple plot overlay - dataframe" at: http://maths.newcastle.edu.au/~rking/R/help/03a/2035.html HTH, Jerome On April 10, 2003 12:39 pm, Feng Zhang wrote:> Hi, R-listers > > I tried to plot several graphs in a sigle x-y coordinate settings, like > the > > following: > |(y) s > | ****** s > | ***** s > | sssssssssssssssssss > |_______________________________(x) > > where "*" and "s" denote two diffrent plots. > > However, when I used > plot(data1); % data1 is the data points of "*" > par(new=T); > plot(data2); % data2 is the data points of "s" > > I found that the x and y labels are messed up, since different graphs > use diffrent unit length on the x-axis and y-axis. > > So is there someway to avoid this problem? > Or is there some other function plottting multiple plots in > one x-y axis setting? > > Thanks for your point and help. > > Fred > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Thanks, Iyue But here the data1 and data2 are not necessarily two vectors. Maybe they are results from some other functions, defined as a data structure. Like using norMix package, x <- norMix(mu=c(-1,0,1.5),sig2=c(.49,.36,.16),w=c(.4,.3,.3)); plot(x); %% x is not a vector, but a structure y <- norMix(mu=c(-2,0,2),sig2=c(.16,.36,.64),w=c(.4,.3,.3)); par(new=T); plot(y); The result graph is messed up and x,y dont have consistent axis label settings. Fred ----- Original Message ----- From: "Iyue Sung" <isung at epidemiology.com> To: "Feng Zhang" <f0z6305 at labs.tamu.edu> Sent: Thursday, April 10, 2003 2:55 PM Subject: RE: [R] How to plot several graphs in a single 2-D figure? Fred, Try:>xrange<-range(c(data1[,1],data2[,1])) >yrange<-range(c(data1[,2],data2[,2])) > >plot(data1, xlim=xrange, ylim=yrange, pch=1) >points(data2, pch=2)-Iyue> -----Original Message----- > From: Feng Zhang [mailto:f0z6305 at labs.tamu.edu] > Sent: Thursday, April 10, 2003 3:39 PM > To: R-Help > Subject: [R] How to plot several graphs in a single 2-D figure? > > > Hi, R-listers > > I tried to plot several graphs in a sigle x-y coordinate > settings, like the > following: > |(y) s > | ****** s > | ***** s > | sssssssssssssssssss > |_______________________________(x) > where "*" and "s" denote two diffrent plots. > > However, when I used > plot(data1); % data1 is the data points of "*" > par(new=T); > plot(data2); % data2 is the data points of "s" > > I found that the x and y labels are messed up, since > different graphs use > diffrent unit length on the x-axis and y-axis. > > So is there someway to avoid this problem? > Or is there some other function plottting multiple plots in > one x-y axis setting? > > Thanks for your point and help. > > Fred > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
----- Original Message ----- From: "Iyue Sung" <isung at epidemiology.com> To: "Feng Zhang" <f0z6305 at labs.tamu.edu> Sent: Thursday, April 10, 2003 2:55 PM Subject: RE: [R] How to plot several graphs in a single 2-D figure? Fred, Try:>xrange<-range(c(data1[,1],data2[,1])) >yrange<-range(c(data1[,2],data2[,2])) > >plot(data1, xlim=xrange, ylim=yrange, pch=1) >points(data2, pch=2)-Iyue> -----Original Message----- > From: Feng Zhang [mailto:f0z6305 at labs.tamu.edu] > Sent: Thursday, April 10, 2003 3:39 PM > To: R-Help > Subject: [R] How to plot several graphs in a single 2-D figure? > > > Hi, R-listers > > I tried to plot several graphs in a sigle x-y coordinate > settings, like the > following: > |(y) s > | ****** s > | ***** s > | sssssssssssssssssss > |_______________________________(x) > where "*" and "s" denote two diffrent plots. > > However, when I used > plot(data1); % data1 is the data points of "*" > par(new=T); > plot(data2); % data2 is the data points of "s" > > I found that the x and y labels are messed up, since > different graphs use > diffrent unit length on the x-axis and y-axis. > > So is there someway to avoid this problem? > Or is there some other function plottting multiple plots in > one x-y axis setting? > > Thanks for your point and help. > > Fred > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
Peter Dalgaard BSA
2003-Apr-10 20:15 UTC
[R] How to plot several graphs in a single 2-D figure?
"Feng Zhang" <f0z6305 at labs.tamu.edu> writes:> Hi, R-listers > > I tried to plot several graphs in a sigle x-y coordinate settings, like the > following: > |(y) s > | ****** s > | ***** s > | sssssssssssssssssss > |_______________________________(x) > where "*" and "s" denote two diffrent plots. > > However, when I used > plot(data1); % data1 is the data points of "*" > par(new=T); > plot(data2); % data2 is the data points of "s" > > I found that the x and y labels are messed up, since different graphs use > diffrent unit length on the x-axis and y-axis. > > So is there someway to avoid this problem? > Or is there some other function plottting multiple plots in > one x-y axis setting?There are several. I assume data1 and data2 are data frames with an x and a y column? Then you might do plot(rbind(data1,data2),type="n") points(data1,pch=1) points(data2,pch=2) Another way is to do what you already did, but add explicit xlim and ylim arguments. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
1. Use xlim and ylim in plot(...) to make sure the axes are the same. 2. For overlaying graphs, use plot(..., axes=FALSE, xlab="", ylab="") to suppress the axes and labels. Andy> -----Original Message----- > From: Feng Zhang [mailto:f0z6305 at labs.tamu.edu] > Sent: Thursday, April 10, 2003 4:03 PM > To: Iyue Sung > Cc: R-Help > Subject: Re: [R] How to plot several graphs in a single 2-D figure? > > > Thanks, Iyue > > But here the data1 and data2 are not necessarily two vectors. > Maybe they are results from some other functions, defined > as a data structure. > Like using norMix package, > > x <- norMix(mu=c(-1,0,1.5),sig2=c(.49,.36,.16),w=c(.4,.3,.3)); > plot(x); %% x is not a vector, but a structure > y <- norMix(mu=c(-2,0,2),sig2=c(.16,.36,.64),w=c(.4,.3,.3)); > par(new=T); > plot(y); > > The result graph is messed up and x,y dont have consistent axis label > settings. > > Fred > > ----- Original Message ----- > From: "Iyue Sung" <isung at epidemiology.com> > To: "Feng Zhang" <f0z6305 at labs.tamu.edu> > Sent: Thursday, April 10, 2003 2:55 PM > Subject: RE: [R] How to plot several graphs in a single 2-D figure? > > > > Fred, Try: > > >xrange<-range(c(data1[,1],data2[,1])) > >yrange<-range(c(data1[,2],data2[,2])) > > > >plot(data1, xlim=xrange, ylim=yrange, pch=1) > >points(data2, pch=2) > > -Iyue > > > -----Original Message----- > > From: Feng Zhang [mailto:f0z6305 at labs.tamu.edu] > > Sent: Thursday, April 10, 2003 3:39 PM > > To: R-Help > > Subject: [R] How to plot several graphs in a single 2-D figure? > > > > > > Hi, R-listers > > > > I tried to plot several graphs in a sigle x-y coordinate > > settings, like the > > following: > > |(y) s > > | ****** s > > | ***** s > > | sssssssssssssssssss > > |_______________________________(x) > > where "*" and "s" denote two diffrent plots. > > > > However, when I used > > plot(data1); % data1 is the data points of "*" > > par(new=T); > > plot(data2); % data2 is the data points of "s" > > > > I found that the x and y labels are messed up, since > > different graphs use > > diffrent unit length on the x-axis and y-axis. > > > > So is there someway to avoid this problem? > > Or is there some other function plottting multiple plots in > > one x-y axis setting? > > > > Thanks for your point and help. > > > > Fred > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >------------------------------------------------------------------------------
Hi, you are definitely looking for functions like points() and lines(). E.g. do plot(x) lines(y) lines() uses the previous coordinate system (defined by plot()) and does not draw any axes. Of course, x and y should have rougly the same range (consider also plot(x, xlim=..., ylim=...). If the scales are very different, you should rescale e.g. y and draw a new axis (look ?axis). Perhaps it helps. Ott | From: "Feng Zhang" <f0z6305 at labs.tamu.edu> | Date: Thu, 10 Apr 2003 15:03:24 -0500 | | Thanks, Iyue | | But here the data1 and data2 are not necessarily two vectors. | Maybe they are results from some other functions, defined | as a data structure. | Like using norMix package, | | x <- norMix(mu=c(-1,0,1.5),sig2=c(.49,.36,.16),w=c(.4,.3,.3)); | plot(x); %% x is not a vector, but a structure | y <- norMix(mu=c(-2,0,2),sig2=c(.16,.36,.64),w=c(.4,.3,.3)); | par(new=T); | plot(y); | | The result graph is messed up and x,y dont have consistent axis label | settings. | | Fred