[R] Reference Lines Using Grid Graphics

Gabor Grothendieck ggrothendieck at gmail.com
Fri Mar 11 23:29:52 CET 2011


On Fri, Mar 11, 2011 at 4:50 PM, Pete Brecknock <Peter.Brecknock at bp.com> wrote:
> Hi
>
> I would like to be able to add reference lines to a series of plots that are
> built using the Grid graphics package. These lines should coincide with tick
> marks which are different on each plot.
>
> I can add the lines manually using the grid.lines() function but would like
> to understand how to generate them on the fly.
>
> Any help would be gratefully received.
>
> Kind regards
>
> Pete
>
>
> #######################################################################
> # FUNCTION MYPLOT
> #######################################################################
> myplot=function(i,j){
>  pushViewport(viewport(layout.pos.col=i,layout.pos.row=j))
>  pushViewport(plotViewport(c(3,2,2,3)))
>
>  x <- runif(10)
>  y <- (i+j)*runif(10)
>
>  pushViewport(dataViewport(range(x),pretty(y),name="plotRegion"))
>
>  grid.points(x, y)
>  grid.rect()
>  grid.xaxis()
>  grid.yaxis(main=FALSE)
>
>  # How can I use grid.lines to create reference lines at the tick marks for
> each graph?
>  # grid.lines(x=0:1,y=0.2)
>
>  grid.text(paste("Plot ",i," ",j,sep=""), y = unit(1, "npc") + unit(0.5,
> "lines"),  gp = gpar(fontsize = 16))
>  popViewport()
>  popViewport(2)
> }
> #######################################################################
> # END FUNCTION MYPLOT
> #######################################################################
>
>
> grid.newpage()
> grid.rect(gp=gpar(fill="white"))
> pushViewport(viewport(layout=grid.layout(2, 2)))
> myplot(1,1)
> myplot(1,2)
> myplot(2,1)
> myplot(2,2)
> popViewport()
>

Try this:

 for(yy in pretty(y)) grid.lines(0:1, unit(yy, "native"))


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list