determining plot location in lattice

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

determining plot location in lattice

Sundar Dorai-Raj
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: determining plot location in lattice

Greg Snow-2
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
[hidden email]
801.408.8111


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Sundar Dorai-Raj
> Sent: Friday, November 07, 2008 12:43 PM
> To: [hidden email]
> 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
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: determining plot location in lattice

Sundar Dorai-Raj
Hi, Greg,

Thanks again for your reply. I looked at TkIdentify which lead me to
"plt", which with RSiteSearch("grid plt") lead me to gridBase::gridPLT.
Here's my solution:


stopifnot(require(tkrplot),
           require(lattice),
           require(gridBase))
makePlot <- function() {
   print(xyplot(1 ~ 1))
   if(click) {
     trellis.focus(highlight = FALSE)
     plt <- gridPLT()
     out <- ((x < plt[1] || x > plt[2]) ||
             (y < plt[3] || y > plt[4]))
     cat(if(out) "outside" else "inside", "\n")
   }
}
clickPlot <- function(x, y) {
   width  <- as.numeric(tclvalue(tkwinfo("reqwidth", img)))
   height <- as.numeric(tclvalue(tkwinfo("reqheight", img)))
   x <<- as.numeric(x)/width
   y <<- 1 - as.numeric(y)/height
   click <<- TRUE
   tkrreplot(img)
   click <<- FALSE
}
x <- y <- -1
click <- FALSE
tt <- tktoplevel()
img <- tkrplot(tt, makePlot)
tkbind(img, "<1>", clickPlot)
tkpack(img)
tkwm.resizable(tt, 0, 0)


Greg Snow said the following on 11/7/2008 8:23 PM:

> 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
> [hidden email]
> 801.408.8111
>
>
>> -----Original Message-----
>> From: [hidden email] [mailto:r-help-bounces@r-
>> project.org] On Behalf Of Sundar Dorai-Raj
>> Sent: Friday, November 07, 2008 12:43 PM
>> To: [hidden email]
>> 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
>>
>> ______________________________________________
>> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: determining plot location in lattice

Greg Snow-2
Glad I could be of help.

Some possible enhancements that you may want to try:

I would wrap all of that into a function so that the assignments using <<- don't put stuff into the global environment but just change things in the top level functions local environment.

Rather than having the click plot function do a replot I would have your makePlot function export the PLT coordinates to the top level function, then clickPlot would just need to compare to those values without replotting each time (you can have other controls replotting if desired).

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
801.408.8111

> -----Original Message-----
> From: Sundar Dorai-Raj [mailto:[hidden email]]
> Sent: Saturday, November 08, 2008 9:17 AM
> To: Greg Snow
> Cc: [hidden email]
> Subject: Re: [R] determining plot location in lattice
>
> Hi, Greg,
>
> Thanks again for your reply. I looked at TkIdentify which lead me to
> "plt", which with RSiteSearch("grid plt") lead me to gridBase::gridPLT.
> Here's my solution:
>
>
> stopifnot(require(tkrplot),
>            require(lattice),
>            require(gridBase))
> makePlot <- function() {
>    print(xyplot(1 ~ 1))
>    if(click) {
>      trellis.focus(highlight = FALSE)
>      plt <- gridPLT()
>      out <- ((x < plt[1] || x > plt[2]) ||
>              (y < plt[3] || y > plt[4]))
>      cat(if(out) "outside" else "inside", "\n")
>    }
> }
> clickPlot <- function(x, y) {
>    width  <- as.numeric(tclvalue(tkwinfo("reqwidth", img)))
>    height <- as.numeric(tclvalue(tkwinfo("reqheight", img)))
>    x <<- as.numeric(x)/width
>    y <<- 1 - as.numeric(y)/height
>    click <<- TRUE
>    tkrreplot(img)
>    click <<- FALSE
> }
> x <- y <- -1
> click <- FALSE
> tt <- tktoplevel()
> img <- tkrplot(tt, makePlot)
> tkbind(img, "<1>", clickPlot)
> tkpack(img)
> tkwm.resizable(tt, 0, 0)
>
>
> Greg Snow said the following on 11/7/2008 8:23 PM:
> > 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
> > [hidden email]
> > 801.408.8111
> >
> >
> >> -----Original Message-----
> >> From: [hidden email] [mailto:r-help-bounces@r-
> >> project.org] On Behalf Of Sundar Dorai-Raj
> >> Sent: Friday, November 07, 2008 12:43 PM
> >> To: [hidden email]
> >> 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
> >>
> >> ______________________________________________
> >> [hidden email] 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.

______________________________________________
[hidden email] 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.