Hi R-Users, I am trying to plot two time series in the same plot, but they measure different things and hence one has values around 1-9 (Use=c(7,8, 6, 2, 3)), and the other one around 20-100 (Resitance=c(80, 100, 95, 35, 28)). I have thought of plotting both in the same graph but with two axes, one from 1 to 9 and the other from 20 to 100. To do so, I needed to do a regression for corrsepondence (1 goes to 20 and 9 goes to 100); the code to produce the graph would be: plot(1996:2000, xlim=c(1996, 2000),ylab="Resistence", ylim=c(20,100), type="n", xlab="Date") lines(1996:2000, c(80, 100, 95, 35, 28), col=1) axis(side=4, at=c(20,30,40,50,60,70,80,90,100), labels=c(1:9)) lines(1996:2000, lsfit(c(1,9),c(20,100))$coef[1]+ lsfit(c(1,9),c(20,100))$coef[2]*c(7,8,6, 2, 3), col=2) legend(1998.5, 90, legend=c("Resistence", "Use"), fill=c(1,2)) However, I suspect there are better ways to do so, and I would like to know one because I have to do that many times. Thanks a lot in advance, Berta
Hi I use a function plot.yy which i designed for convenieant plotting on 2 y axes for myself (see code below). You can modify its internals to suit your needs easily but this will give you something quite close. plot.yy(1996:2000, c(80, 100, 95, 35, 28), c(7,8,6, 2, 3), xlim=c(1996, 2000), yylab=c("Resistence","Use"), xlab="Date", pch=c(NA,NA), linky=T) HTH Petr On 2 Mar 2007 at 11:54, Berta wrote: From: "Berta" <ibanez at bioef.org> To: <r-help at stat.math.ethz.ch> Date sent: Fri, 2 Mar 2007 11:54:57 +0100 Organization: bioef Subject: [R] plot of 2 time series with very different values> > Hi R-Users, > > I am trying to plot two time series in the same plot, but they measure > different things and hence one > has values around 1-9 (Use=c(7,8, 6, 2, 3)), and the other one around > > 20-100 (Resitance=c(80, 100, 95, 35, 28)). I have thought of plotting > both in the same graph but with two axes, one from 1 to 9 and the > other from 20 to 100. To do so, I needed to do a regression for > corrsepondence (1 goes to 20 and 9 goes to 100); the code to produce > the graph would be: > > plot(1996:2000, xlim=c(1996, 2000),ylab="Resistence", ylim=c(20,100), > type="n", xlab="Date") lines(1996:2000, c(80, 100, 95, 35, 28), col=1) > axis(side=4, at=c(20,30,40,50,60,70,80,90,100), labels=c(1:9)) > lines(1996:2000, lsfit(c(1,9),c(20,100))$coef[1]+ > lsfit(c(1,9),c(20,100))$coef[2]*c(7,8,6, 2, 3), col=2) legend(1998.5, > 90, legend=c("Resistence", "Use"), fill=c(1,2)) > > However, I suspect there are better ways to do so, and I would like to > know one because I have to do that many times. > > Thanks a lot in advance, > > Berta > > ______________________________________________ > 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.Here is the code, all parameters are easily understood except of rect, which will was designed for a plotting a rectangle and you can ignore it completely. 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/%m", rect=NULL, type="p",...) { 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], type=type, ...) if (!is.null(rect)) rect(x[rect[1]],rect[2],cas.osa[rect[3]],rect[4], col="grey") points(x, yright, ylim=yrightlim, 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 (!inherits(x,c("Date","POSIXt"))) 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=length)]),format=format)) } 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, ...) } Petr Pikal petr.pikal at precheza.cz
Thank you so much Petr, it is exaclty what I was looking for!! Berta.> Hi > > I use a function plot.yy which i designed for convenieant plotting on > 2 y axes for myself (see code below). You can modify its internals to > suit your needs easily but this will give you something quite close. > > plot.yy(1996:2000, c(80, 100, 95, 35, 28), c(7,8,6, 2, 3), > xlim=c(1996, 2000), > yylab=c("Resistence","Use"), xlab="Date", pch=c(NA,NA), linky=T) > > HTH > Petr > > On 2 Mar 2007 at 11:54, Berta wrote: > > From: "Berta" <ibanez at bioef.org> > To: <r-help at stat.math.ethz.ch> > Date sent: Fri, 2 Mar 2007 11:54:57 +0100 > Organization: bioef > Subject: [R] plot of 2 time series with very different values > >> >> Hi R-Users, >> >> I am trying to plot two time series in the same plot, but they measure >> different things and hence one >> has values around 1-9 (Use=c(7,8, 6, 2, 3)), and the other one around >> >> 20-100 (Resitance=c(80, 100, 95, 35, 28)). I have thought of plotting >> both in the same graph but with two axes, one from 1 to 9 and the >> other from 20 to 100. To do so, I needed to do a regression for >> corrsepondence (1 goes to 20 and 9 goes to 100); the code to produce >> the graph would be: >> >> plot(1996:2000, xlim=c(1996, 2000),ylab="Resistence", ylim=c(20,100), >> type="n", xlab="Date") lines(1996:2000, c(80, 100, 95, 35, 28), col=1) >> axis(side=4, at=c(20,30,40,50,60,70,80,90,100), labels=c(1:9)) >> lines(1996:2000, lsfit(c(1,9),c(20,100))$coef[1]+ >> lsfit(c(1,9),c(20,100))$coef[2]*c(7,8,6, 2, 3), col=2) legend(1998.5, >> 90, legend=c("Resistence", "Use"), fill=c(1,2)) >> >> However, I suspect there are better ways to do so, and I would like to >> know one because I have to do that many times. >> >> Thanks a lot in advance, >> >> Berta >> >> ______________________________________________ >> 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. > > Here is the code, all parameters are easily understood except of > rect, which will was designed for a plotting a rectangle and you can > ignore it completely. > > 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/%m", rect=NULL, type="p",...) > > { > > 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], type=type, ...) > if (!is.null(rect)) rect(x[rect[1]],rect[2],cas.osa[rect[3]],rect[4], > col="grey") > points(x, yright, ylim=yrightlim, 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 (!inherits(x,c("Date","POSIXt"))) > 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=length)]),format=format)) > } > > 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, ...) > > } > Petr Pikal > petr.pikal at precheza.cz > > > >
Gabor Grothendieck
2007-Mar-02 15:30 UTC
[R] plot of 2 time series with very different values
There is an example in the example section of plotting two time series on the same plot with different left hand and right hand scales here: library(zoo) ?plot.zoo On 3/2/07, Berta <ibanez at bioef.org> wrote:> > Hi R-Users, > > I am trying to plot two time series in the same plot, but they measure > different things and hence one > has values around 1-9 (Use=c(7,8, 6, 2, 3)), and the other one around > 20-100 (Resitance=c(80, 100, 95, 35, 28)). I have thought of plotting both > in the same graph but with two axes, one from 1 to 9 and the other from 20 > to 100. To do so, I needed to do a regression for corrsepondence (1 goes to > 20 and 9 goes to 100); the code to produce the graph would be: > > plot(1996:2000, xlim=c(1996, 2000),ylab="Resistence", ylim=c(20,100), > type="n", xlab="Date") > lines(1996:2000, c(80, 100, 95, 35, 28), col=1) > axis(side=4, at=c(20,30,40,50,60,70,80,90,100), labels=c(1:9)) > lines(1996:2000, lsfit(c(1,9),c(20,100))$coef[1]+ > lsfit(c(1,9),c(20,100))$coef[2]*c(7,8,6, 2, 3), col=2) > legend(1998.5, 90, legend=c("Resistence", "Use"), fill=c(1,2)) > > However, I suspect there are better ways to do so, and I would like to know > one because I have to do that many times. > > Thanks a lot in advance, > > Berta > > ______________________________________________ > 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. >