# Re: [R] hexbin and grid - input data values as coordinates

Date: Sat 02 Apr 2005 - 05:33:42 EST

I spent the afternoon trying to code up something that might be used as grid.abline() and grid.grid() before I read Martin's suggestion. Sigh.

But here it is anyway in case you can salvage something out of my inelegant solution.

mygrid.abline <- function(a=NULL, b=NULL, h=NULL, v=NULL,

```                          vps, gp=gpar(col=1, lwd=1) ) {

```

# a, b, h and v are as documented in help(abline)   # vps and gp are the viewport and its graph parameters

if(!is.null(h)){ a <- h; b <- 0 }
if(!is.null(v)){ a <- v; b <- Inf }

pushHexport(vps\$plot.vp)

xmin <- vps\$plot.vp@xscale[1]; xmax <- vps\$plot.vp@xscale[2]   ymin <- vps\$plot.vp@yscale[1]; ymax <- vps\$plot.vp@yscale[2]

```  x0 <- max( (ymin - a)/b, xmin )
x0 <- min( x0, xmax )
y0 <- a + b*x0

x1 <- min( (ymax - a)/b, xmax )
```

x1 <- max( x1, xmin )
y1 <- a + b*x1

if( !is.finite(b) ){ # fudge for vertical lines     x0 <- a; x1 <- a
y0 <- ymin; y1 <- ymax
}

grid.move.to( x0, y0, default.units="native" )   grid.line.to( x1, y1, default.units="native", gp=gp )

popViewport()
invisible( c( x0=x0, y0=y0, x1=x1, y1=y1 ) ) }

mygrid.grid <- function(vps, nx=NULL, ny=nx,

```                        gp=gpar(col=8, lwd=1, lty=2)){

```

xmin <- vps\$plot.vp@xscale[1]; xmax <- vps\$plot.vp@xscale[2]   ymin <- vps\$plot.vp@yscale[1]; ymax <- vps\$plot.vp@yscale[2]

if( is.null(nx) ){ # the default

vv <- seq(as.integer(xmin), as.integer(xmax), by=1)     hh <- seq(as.integer(ymin), as.integer(ymax), by=1)

} else {

vv <- seq( xmin, xmax, length.out = nx + 1 )     hh <- seq( ymin, ymax, length.out = ny + 1 )

}

sapply( vv, function(v) mygrid.abline( v=v, vps=vps, gp=gp ) )   sapply( hh, function(h) mygrid.abline( h=h, vps=vps, gp=gp ) )   invisible()
}

# USAGE EXAMPLE
x <- rnorm(1000)
y <- 10 + 1.6*x + rnorm(1000)

vps <- plot( hexbin( x, y ), style = "nested.lattice")

```mygrid.abline( a=10, b=2, vps=vps,  gp=gpar(col=2, lwd=3) )
mygrid.abline( a=10, b=-2, vps=vps, gp=gpar(col=1, lty=2) )
mygrid.grid( vps )

```

On Fri, 2005-04-01 at 14:40 +0200, Martin Maechler wrote:
> >>>>> "Paul" == Paul Murrell <p.murrell@auckland.ac.nz>
> >>>>> on Fri, 01 Apr 2005 10:45:16 +1200 writes:
>
> Paul> Hi Adaikalavan Ramasamy wrote:
> >> Dear all,
> >>
> >> I am trying to use hexbin and read the very interesting
> >> article on grid (
> >> http://www.ci.tuwien.ac.at/Conferences/useR-2004/Keynotes/Murrell.pdf
> >> ) and am hoping for some advice from more experienced
> >> users of hexbin.
> >>
> >> I am trying to visualise a data and fit a straight line
> >> trough it. For example, here is how I would do it in the
> >> usual way
> >>
> >> # simulate data x <- rnorm(1000) y <- 5*x + rnorm(1000,
> >> sd=0.5)
> >>
> >> plot( x, y, pch="*" ) abline(0, 1, col=2)
> >>
> >>
> >> And here is my failed attempt at fitting the "abline" on
> >> hexbin
> >>
> >> library(hexbin); library(grid) plot( hexbin( x, y ),
> >> style = "nested.lattice") grid.move.to(0.2,0.2)
> >> grid.line.to(0.8,0.8)
> >>
> >> I realise that grid.* is taking plotting coordinates on
> >> the graph but how do I tell it to use the coordinates
> >> based on the data values ? For my real data, I would like
> >> lines with different slopes and intercepts.
>
>
> Paul> gplot.hexbin() returns the viewports it used to
> Paul> produce the plot and the legend. Here's an example of
> Paul> annotating the plot ...
>
> Paul> # capture the viewports returned vps <- plot(
> Paul> hexbin( x, y ), style = "nested.lattice") # push the
> Paul> viewport corresponding to the plot # this is actually
> Paul> a hexViewport rather than a plain grid viewport # so
> Paul> you use pushHexport rather than grid's pushViewport
> Paul> pushHexport(vps\$plot.vp) # use "native" coordinates to
> Paul> draw relative to the axis scales grid.move.to(-2, -10,
> Paul> default.units="native") grid.line.to(2, 10,
> Paul> default.units="native", gp=gpar(col="yellow", lwd=3))
> Paul> # tidy up popViewport()
>
> Paul> There's another annotation example at the bottom of
> Paul> the help page for gplot.hexbin
>
> Paul> A grid.abline() function would obviously be a useful
> Paul> addition. Must find where I put my todo list ...
>
> well, it seems to me that if you start with panel.abline() from
> lattice, you're almost finished right from start.
>
> But then, sometimes the distance between "almost" and
> "completely" can become quite large...
>
> Further, from the looks of it, if you finish it, panel.abline()
> could become a simple wrapper around grid.abline().
>
>
> Martin
>
>
> Paul> Paul
>
> >> I am using the hexbin version 1.2-0 ( which is the devel
> >> version ), R-2.0.1 and Fedora Core 3.
> >>
> >> Many thanks in advance.
> >>