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

From: Paul Murrell <p.murrell_at_auckland.ac.nz>
Date: Fri 01 Apr 2005 - 08:45:16 EST

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.

gplot.hexbin() returns the viewports it used to produce the plot and the legend. Here's an example of annotating the plot ...

  # capture the viewports returned
  vps <- plot( hexbin( x, y ), style = "nested.lattice")

  # push the viewport corresponding to the plot
  # this is actually a hexViewport rather than a plain grid viewport
  # so you use pushHexport rather than grid's pushViewport
  pushHexport(vps$plot.vp)
  # use "native" coordinates to draw relative to the axis scales   grid.move.to(-2, -10, default.units="native")   grid.line.to(2, 10, default.units="native",

               gp=gpar(col="yellow", lwd=3))   # tidy up
  popViewport()

There's another annotation example at the bottom of the help page for gplot.hexbin

A grid.abline() function would obviously be a useful addition. Must find where I put my todo list ...

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
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul@stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Fri Apr 01 08:51:05 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:58 EST