[R] adding a second axis to a plot

Petr Pikal petr.pikal at precheza.cz
Tue Apr 27 18:21:25 CEST 2004


Hi

You have quite many options

plot()
lines()
points()

with appropriate scaling

or
par(new=TRUE)
before calling second plot

or

matplot()

see documentation to functions

Depends on what you want to plot.

I use following function - plot.yy(x,y1,y2)

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[s
eq(1,l,length=length)]),format=format))
}


#if (xDatum) 
axis(1,dates(pretty(range(datum,na.rm=T),10)),labels=as.characte
r(chron(pretty(range(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, ...)

}


Cheers
Petr



On 27 Apr 2004 at 16:38, Luis Rideau Cruz wrote:

> How to plot something and then
> add a new axis for a new vector to be plotted?
> 
> Luis Ridao Cruz
> Fiskirannsóknarstovan
> Nóatún 1
> P.O. Box 3051
> FR-110 Tórshavn
> Faroe Islands
> Phone:             +298 353900
> Phone(direct): +298 353912
> Mobile:             +298 580800
> Fax:                 +298 353901
> E-mail:              luisr at frs.fo
> Web:                www.frs.fo
> 
> ______________________________________________
> 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