[R] High/low level: Plot 2 time series with different axis (left and right)
Petr Pikal
petr.pikal at precheza.cz
Mon Mar 15 08:06:47 CET 2004
Hi
On 14 Mar 2004 at 18:04, Jan Verbesselt wrote:
> Dear R specialists,
>
> I have two time series in a data.frame and want to plot them in the
> same plot(), with the left axis scaled to time series 1 (-700,0) and
> the right axis scaled to time series 2 (-0.2, 0.4).
>
> plot(timeserie1)
> lines(timeserie2, col=c(2)) => this one should be scaled differently
> with a new axis on the right handside.
I am not really a R specialist but for this task I use function:
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
{
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, ...)
}
### End of a function
It gives you a limited possibility to do some smoothing, add lines
change colors and points and add some axes annotation and it handels x
in POSIX class }just a little bit :).
Cheers
Petr
>
> How can these be visualised such that the fit is optimal for
> visualisation of the two time series? Which commands can I use?
>
> Thanks,
> Jan
>
> ______________________________________________
> 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
Petr Pikal
petr.pikal at precheza.cz
More information about the R-help
mailing list