Re: [R] locator

From: tom wright <tom_at_maladmin.com>
Date: Sat 22 Oct 2005 - 02:14:22 EST

Perfect, thankyou

On Fri, 2005-21-10 at 15:46 -0400, Duncan Murdoch wrote:
> On 10/21/2005 11:23 AM, tom wright wrote:
> > I'm trying to use the locator function on a drawing area with multiple
> > graphs par(mfrow=c(1,2))
> > Is it possible to identify which graph has been clicked?
>
> locator() will return coordinates based on the active graph (typically
> the last one you drew), so you can work it out from that.
> That is, convert from user coordinates to screen coordinates, and then
> work out the position from there:
>
> getcell <- function(pts = locator()) {
> usr <- par("usr")
> plt <- par("plt")
> mfg <- par("mfg")
>
> x <- pts$x
> y <- pts$y
>
> # convert to 0-1 plot coordinates
> px <- (x-usr[1])/(usr[2]-usr[1])
> py <- (y-usr[3])/(usr[4]-usr[3])
>
> # convert to 0-1 frame coordinates
> fx <- plt[1] + px*(plt[2]-plt[1])
> fy <- plt[3] + py*(plt[4]-plt[3])
>
> # convert to a relative cell position
> cx <- floor(fx)
> cy <- floor(fy)
>
> # return the absolute cell position
> list(row = -cy+mfg[1],col = cx+mfg[2])
> }
>



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 Sat Oct 22 06:18:16 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 19:16:29 EST