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

Martin Maechler maechler at stat.math.ethz.ch
Fri Apr 1 14:40:42 CEST 2005


>>>>> "Paul" == Paul Murrell <p.murrell at 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.
    >> 
    >> Regards, Adai




More information about the R-help mailing list