[R] plot

Petr Pikal petr.pikal at precheza.cz
Mon Mar 17 12:12:20 CET 2003


Hi

On 17 Mar 2003 at 9:06, Morten Sickel wrote:

> noliveir2003 at zipmail.com.br [mailto:noliveir2003 at zipmail.com.br]
> wrote:
> 
> >Can anyone tell me how to plot on the same graph two different
> >functions (of x) using two differnt y scales (axes 2 and 4)?
> 
> The only way I've found is to use the grid library and
> push.viewport(). ((...) means substitute with whatever appropriate,
> use help())
> 
> library(grid)
> grid.newpage()
> push.viewport(viewport(yscale=c(0,500),w=.75,h=.75,xscale=c(0:100)))
> grid.poins(...) # Plots data point with an y-scale of 0-500
> grid.xaxis(...) grid.yaxis(...) # Draws the left y-axis
> push.viewport(viewport(yscale=c(0,10),xscale=c(0:100)))
> grid.poins(...) # Plots data point with an y-scale of 0-10
> grid,yaxis(main=FALSE,...) # Draws the right y-axis

Or maybe this small function which I use for 2 (different scale) y axes can make it 
on ordinary graphic device.

You has to provide 3 vectors x, yright and yleft and you can put lines through 
linky=T or/and smoothed lines through setting smooth > 0.

plot.yy<-function(x,yright,yleft, xlab = NULL 
,yylab=c("",""),pch=c(1,2),col=c(1,2), linky=F, smooth=0, lwds=1, ...)

{

par(mar=c(5,4,4,2),oma=c(0,0,0,3))
plot(x,yright,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,ylab="", axes=F ,xlab=xlab, pch=pch[2],col=col[2], ...)
box()
axis(2,pretty(range(yleft,na.rm=T),10),col=col[2])
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, 
...)

}



> 
> Morten
> 
> -- 
> Morten Sickel
> Norwegian Radiation Protection Authority
> http://www.nrpa.no
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

Cheers

Petr Pikal
petr.pikal at precheza.cz
p.pik at volny.cz



More information about the R-help mailing list