Wittner, Ben, Ph.D.

2008-Jun-01 18:50 UTC

### [R] transforming output of grid.locator() to coordinates of a leaf viewport

Short form: How do I transform the output of grid.locator() (or grid.locator(unit='npc')) to the native (or npc) coordinates of a viewport other than the top-level viewport? Thanks in advance. -Ben Long form: I would like the user to be able to click anywhere on the R graphics window. I would then like to figure out which leaf viewport the user clicked in and compute the native coordinates in that leaf viewport of the user's click. I saw a 2005 response by Paul Murrell to a post by Gabor Grothendieck roughly on this topic in which Paul suggested looking at trellis.focus(). I've looked at trellis.focus() and trellis.clickFocus(), but they don't appear to do what I want in that a) it would appear that my user would have to click twice, once to specify which leaf viewport and then again to specify which point in that viewport and b) trellis.clickFocus() appears to use lots of information from the trellis package, which I'm not using. So I decided to try to figure out the native coordinates of the click in each leaf viewport, to a) see whether the click is within the plotting region for that viewport and b) use those coordinates to take the appropriate action if it is. But I don't seem to be able to figure out how to translate from the top-level viewport coordinates to those of a leaf viewport. I tried interpreting the 3X3 matrix returned by current.transform() as an affine transformation in the x,y plane in homogeneous coordinates, but could make no sense of what I was getting. Thanks for any help. -Ben The information transmitted in this electronic communica...{{dropped:16}}

hadley wickham

2008-Jun-01 19:24 UTC

### [R] transforming output of grid.locator() to coordinates of a leaf viewport

Ben, What if the click occurs over multiple viewports? Hadley On Mon, Jun 2, 2008 at 6:50 AM, Wittner, Ben, Ph.D. <Wittner.Ben at mgh.harvard.edu> wrote:> Short form: > > How do I transform the output of grid.locator() (or > grid.locator(unit='npc')) to the native (or npc) coordinates of a viewport other > than the top-level viewport? > > Thanks in advance. -Ben > > Long form: > > I would like the user to be able to click anywhere on the R > graphics window. I would then like to figure out which leaf viewport the user > clicked in and compute the native coordinates in that leaf viewport of the > user's click. > > I saw a 2005 response by Paul Murrell to a post by Gabor > Grothendieck roughly on this topic in which Paul suggested looking at > trellis.focus(). I've looked at trellis.focus() and trellis.clickFocus(), but > they don't appear to do what I want in that a) it would appear that my user > would have to click twice, once to specify which leaf viewport and then again to > specify which point in that viewport and b) trellis.clickFocus() appears to use > lots of information from the trellis package, which I'm not using. > > So I decided to try to figure out the native coordinates of the > click in each leaf viewport, to a) see whether the click is within the plotting > region for that viewport and b) use those coordinates to take the appropriate > action if it is. But I don't seem to be able to figure out how to translate from > the top-level viewport coordinates to those of a leaf viewport. I tried > interpreting the 3X3 matrix returned by current.transform() as an affine > transformation in the x,y plane in homogeneous coordinates, but could make no > sense of what I was getting. > > Thanks for any help. -Ben > > > > > The information transmitted in this electronic communica...{{dropped:16}} > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- http://had.co.nz/