hi all i have another probably simple question. I have three variables say x, y and z. x and y are quite large and z is relative small. how can one plot the three variables on the same graph with two separate axis? (one for x and y and the other for z) e.g. x<-c(101,110,150,167,120) y<-c(120,135,175,95,200) z<-c(0.001, 0.15, 0.6, 0.8, 1) regards Allan
allan clark wrote:> hi all > > i have another probably simple question. > > I have three variables say x, y and z. x and y are quite large and z is > relative small. > how can one plot the three variables on the same graph with two separate > axis? > (one for x and y and the other for z) > > e.g.x<-c(101,110,150,167,120) y<-c(120,135,175,95,200) z<-c(0.001, 0.15, 0.6, 0.8, 1) ## Try something like: fac <- 200 par(mar=c(5,4,4,5)+.1) plot(x, ylim=range(c(x, y, fac * z)), col="blue", ylab="x, y") points(y, col="green") points(z*fac, col="red") axis(4, at=pretty(z*fac), label=pretty(z*fac)/fac, ylab="z") mtext("z", side=4, line=2.5) # more about this see ?axis, ?pretty and ?mtext Thomas P. -- Thomas Petzoldt Dresden University of Technology Institute of Hydrobiology petzoldt at rcs.urz.tu-dresden.de 01062 Dresden http://www.tu-dresden.de/fghhihb/
On Tue, 2004-02-24 at 06:44, allan clark wrote:> hi all > > i have another probably simple question. > > I have three variables say x, y and z. x and y are quite large and z is > relative small. > how can one plot the three variables on the same graph with two separate > axis? > (one for x and y and the other for z) > > e.g. > x<-c(101,110,150,167,120) > y<-c(120,135,175,95,200) > z<-c(0.001, 0.15, 0.6, 0.8, 1) > > regards > AllanYou could do something like this: x<-c(101, 110, 150, 167, 120) y<-c(120, 135, 175, 95, 200) z<-c(0.001, 0.15, 0.6, 0.8, 1) # Create a matrix with all three columns # "Normalize" z against the range of the other two m <- cbind(x, y, z * 100) # Now use matplot() to create the plot, which also # helps to ensure that the y axis covers the range # of the three vectors matplot(m, type = "p", pch = c("x", "y", "z")) # Now annotate axis 4 (the right hand y axis) # adjusting the labels back to the original z vector # scaling. Use axTicks() to get the default tick mark # locations and divide the values by 100 axis(4, labels = axTicks(4) / 100) See ?cbind, ?matplot and ?axis and ?axTicks for more information. HTH, Marc Schwartz
On Tue, 24 Feb 2004 14:44:47 +0200, allan clark <allan at stats.uct.ac.za> wrote :>hi all > >i have another probably simple question. > >I have three variables say x, y and z. x and y are quite large and z is >relative small. >how can one plot the three variables on the same graph with two separate >axis? >(one for x and y and the other for z) > >e.g. >x<-c(101,110,150,167,120) >y<-c(120,135,175,95,200) >z<-c(0.001, 0.15, 0.6, 0.8, 1)You need to do most of the work yourself: par(mar=par('mar')+c(0,0,0,2)) # make room for the extra axis plot(1:5, x, ylim = c(0, 200)) # plot x with room for the others points(1:5, y, pch=2) # plot y points(1:5, 200*z, pch=3) # plot z, magnified 200 times zticks <- pretty(z) # choose ticks for z axis(4, at=200*zticks, labels=zticks) # plot them mtext('z',side=4, line=3) # add the z label You'll also want to add a legend, but I'll let you figure that out. Duncan Murdoch
How about: x<-c(101,110,150,167,120) y<-c(120,135,175,95,200) z<-c(0.001, 0.15, 0.6, 0.8, 1) plot(x,y,axes=T,xlab="",ylab="",pch="+") par(new=T) plot(x,z,axes=F,ylim=c(0,1)) axis(4, at=seq(0,1,.1), labels=seq(0,1,.1)) box()> -----Original Message----- > From: Duncan Murdoch [SMTP:dmurdoch at pair.com] > Sent: Tuesday, February 24, 2004 6:10 AM > To: allan clark > Cc: Rhelp > Subject: Re: [R] r: plots > > On Tue, 24 Feb 2004 14:44:47 +0200, allan clark > <allan at stats.uct.ac.za> wrote : > > >hi all > > > >i have another probably simple question. > > > >I have three variables say x, y and z. x and y are quite large and z is > >relative small. > >how can one plot the three variables on the same graph with two separate > >axis? > >(one for x and y and the other for z) > > > >e.g. > >x<-c(101,110,150,167,120) > >y<-c(120,135,175,95,200) > >z<-c(0.001, 0.15, 0.6, 0.8, 1) > > You need to do most of the work yourself: > > par(mar=par('mar')+c(0,0,0,2)) # make room for the extra axis > plot(1:5, x, ylim = c(0, 200)) # plot x with room for the others > points(1:5, y, pch=2) # plot y > points(1:5, 200*z, pch=3) # plot z, magnified 200 times > zticks <- pretty(z) # choose ticks for z > axis(4, at=200*zticks, labels=zticks) # plot them > mtext('z',side=4, line=3) # add the z label > > You'll also want to add a legend, but I'll let you figure that out. > > Duncan Murdoch > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
Hallo I use following function to produce graph of yright and yleft against x with additional options (like smoothing the lines, colour, line and point type choice and an options for some axes formating. Cheers Petr #---------------------------------------------------------------------------------------------- # graf na 2 osach y plot.yy<-function(x,yright,yleft, yleftlim=NULL, yrightlim = NULL, xlab = NULL ,yylab=c("",""),pch=c(1,2),col=c(1,2), linky=F, smooth=0, lwds=1, length=10, format="%d-%H:%M", ...) { par(mar=c(5,4,4,2),oma=c(0,0,0,3)) plot(x, yright, ylim=yrightlim, axes=F,ylab="", xlab=xlab, pch=pch[1],col=col[1], ...) axis(4,pretty(range(yright,na.rm=T),10),col=col[1]) if (linky) lines(x,yright,col=col[1], ...) if (smooth!=0) lines(supsmu(x,yright,span=smooth),col=col[1], lwd=lwds, ...) if(yylab[1]=="") mtext(deparse(substitute(yright)),side=4,outer=T,line=1, col=col[1], ...) else mtext(yylab[1],side=4,outer=T,line=1, col=col[1], ...) par(new=T) plot(x,yleft, ylim=yleftlim, ylab="", axes=F ,xlab=xlab, pch=pch[2],col=col[2], ...) box() axis(2,pretty(range(yleft,na.rm=T),10),col=col[2], col.axis=col[2]) #if (is.null(class(x))) axis(1,pretty(range(x,na.rm=T),10)) else axis.POSIXct(1, x) if (is.null(class(x))) axis(1,pretty(range(x,na.rm=T),10)) else { l<-length(x) axis(1,at=x[seq(1,l,length=length)],labels=format(as.POSIXct(x[seq(1,l,length=le ngth)]),format=format)) } #if (xDatum) axis(1,dates(pretty(range(datum,na.rm=T),10)),labels=as.character(chron(pretty(r ange(datum,na.rm=T),10)),format=c("d/m/y"))) #else axis(1,pretty(range(x,na.rm=T),10)) if(yylab[2]=="") mtext(deparse(substitute(yleft)),side=2,line=2, col=col[2], ...) else mtext(yylab[2],side=2,line=2, col=col[2], ...) if (linky) lines(x,yleft,col=col[2], lty=2, ...) if (smooth!=0) lines(supsmu(x,yleft,span=smooth),col=col[2], lty=2, lwd=lwds, ...) } On 24 Feb 2004 at 14:44, allan clark wrote:> hi all > > i have another probably simple question. > > I have three variables say x, y and z. x and y are quite large and z > is relative small. how can one plot the three variables on the same > graph with two separate axis? (one for x and y and the other for z) > > e.g. > x<-c(101,110,150,167,120) > y<-c(120,135,175,95,200) > z<-c(0.001, 0.15, 0.6, 0.8, 1) > > regards > Allan > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz