Skip to content

locator

3 messages · Tom, Duncan Murdoch

Tom
#
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?
thanks
tom
Tom
#
Perfect, thankyou
On Fri, 2005-21-10 at 15:46 -0400, Duncan Murdoch wrote:
#
On 10/21/2005 11:23 AM, tom wright wrote:
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])
}