Re: [R] locator

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Sat 22 Oct 2005 - 05:46:36 EST

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 05:56:39 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:40:46 EST