Re: [R] determining plot location in lattice

From: Greg Snow <>
Date: Fri, 07 Nov 2008 21:23:41 -0700

As far as R is concerned, the plot in tkrplot is being written to a file and therefore is not interactive. So functions like grid.locator and trellis.focus are not going to work.

You need to use the Tk commands to determine where the mouse is and where a click occurred, then this needs to be converted to the coordinates of the trellis plot. There are some examples of the first part in the TeachingDemos package, eg TkBrush, TkIdentify, TkApprox, and a couple of others (also the play.sudoku function in the Sudoku package). These all work with base graphics rather than trellis/grid so will not help with the 2nd part. Possibly something in the grid package can tell you the coordinates of the various viewports (but you need to save this information before the end of the plotting command because the information will probably be lost when the plot finishes).

Hope this helps,

Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare

> -----Original Message-----
> From: [mailto:r-help-bounces_at_r-
>] On Behalf Of Sundar Dorai-Raj
> Sent: Friday, November 07, 2008 12:43 PM
> To:
> Subject: [R] determining plot location in lattice
> Hi,
> I'm dealing with a lattice plot inserted into a tk widget and would
> like
> to know when a user has clicked on the plot area of a plot (i.e. inside
> the axes). For example,

> library(tkrplot)
> library(lattice)
> tt <- tktoplevel()
> makePlot <- function() print(xyplot(1 ~ 1))
> printCoords <- function(x, y) print(c(x, y))
> img <- tkrplot(tt, makePlot)
> tkbind(img, "<1>", printCoords)
> tkpack(img)

> I would like to know when a user clicks inside the axes, but don't know
> how to determine where the plot region is. Essentially, this comes down
> to answering the following question: Assuming the entire plot is on a
> unit square, what are the coordinates of the plotting area?

> This question is may seem unrelated to tkrplot, but outside of this
> context I may not get answers that work. I have been playing with both
> lattice::trellis.focus and grid::grid.locator to no avail.

> Thanks,
> --sundar
> ______________________________________________
> mailing list
> PLEASE do read the posting guide
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
______________________________________________ mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code.
Received on Sat 08 Nov 2008 - 04:32:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 08 Nov 2008 - 16:30:23 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive