Re: [R] Reference Lines Using Grid Graphics

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri, 11 Mar 2011 17:29:52 -0500

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

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 11 Mar 2011 - 22:34:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 11 Mar 2011 - 22:50:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive