Skip to content
Back to formatted view

Raw Message

Message-ID: <4359459C.9080308@stats.uwo.ca>
Date: 2005-10-21T19:46:36Z
From: Duncan Murdoch
Subject: locator
In-Reply-To: <1129908231.4222.1.camel@localhost.localdomain>

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])
}