[R] locator
Duncan Murdoch
murdoch at stats.uwo.ca
Fri Oct 21 21:46:36 CEST 2005
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])
}
More information about the R-help
mailing list