# [R] r: plots

Petr Pikal petr.pikal at precheza.cz
Wed Feb 25 14:07:16 CET 2004

```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.html

Petr Pikal
petr.pikal at precheza.cz

```