3

tl;dr: Is there a function to get the same output as identify() or locator(), but without a mouse click (say a mouse hover position instead)?

I am generating plots, and saving them to a PNG file, and embedding them into my application. When the user interacts with the image in my application, I'd like to send those screen coordinates back to the graphics device in R to understand where the user is in the data coordinates.

I need a version of identify() or locator() that lets me pass in the mouse coordinates explicitly.

For example, is the user hovering on a point corresponding to year 2015 and birth rate 90, if they are hovering on pixel 1000, 2000?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Neil
  • 3,100
  • 5
  • 29
  • 36
  • 2
    This is >10 years old, so there could well be new developments, but `fortunes::fortune(30)`. – Gregor Thomas Apr 02 '15 at 16:24
  • 1
    Have a look at `?grconvertX`, which -- with enough care -- should allow you should allow you to implement something like this. [Here is an answer](http://stackoverflow.com/questions/14288194/plot-inside-a-plot/14289073#14289073) in which I used it and `grconvertY()` to go from plot (`"user"`) coordinates to normalized device (`"ndc"`) coordinates -- basically the reverse of the operation you'll likely want to use. – Josh O'Brien Apr 02 '15 at 17:05
  • Josh, grconvertX works perfectly! Could you add it as an answer so I mark this as answered to help future searchers? I'm finding that 'dev' or device coordinates give me the exact pixel values for 'user' coordinates, which are the values from the graph, so going the other way around should work. It seems like the ndc coordinates are basically the same, but divided by the width of the image in order to normalise to the range of 0 to 1, is that right? – Neil Apr 12 '15 at 00:14
  • And Gregor, that is the most epic response to a question that I've ever received! That someone 10 years prior to me asked the exact same question, which was considered important enough to be included in a library, that you were able to precisely recall and reference. – Neil Apr 12 '15 at 00:16
  • Hi @zx8754, I did miss that, but don't have time/interest in writing up an answer. Do feel free totally free to take my comment and flesh it out as an answer of your own. – Josh O'Brien Oct 19 '16 at 12:47

1 Answers1

1

Have a look at ?grconvertX, which with enough care should allow you should allow you to implement something like this.

Here is an answer in which I used it and grconvertY() to go from plot ("user") coordinates to normalized device ("ndc") coordinates - basically the reverse of the operation you'll likely want to use.

– Josh O'Brien Apr 2 '15 at 17:05

I'm finding that 'dev' or device coordinates give me the exact pixel values for 'user' coordinates, which are the values from the graph, so going the other way around should work. It seems like the 'ndc' coordinates are basically the same, but divided by the width of the image in order to normalize to the range of 0 to 1.

- Neil Apr 12 '15 at 0:14

Community
  • 1
  • 1
zx8754
  • 52,746
  • 12
  • 114
  • 209