title: locator equivalent in ggplot2 (for maps)
body: <p><em>Note: This question is specific for mapping but I'd like to be able to use it when I plot in a standard Cartesian coordinate system.</em></p>
  
  <p>I love base graphics but also like ggplot2 for many things. One of my most used base functions for fine tuning a graph is locator(n) but this produces an error in ggplot2.</p>
  
  <pre><code>library(ggplot2) 
  county_df &lt;- map_data('county')  #mappings of counties by state
  ny &lt;- subset(county_df, region=="new york")   #subset just for NYS
  ny$county &lt;- ny$subregion
  
  ggplot(ny, aes(long, lat, group=group)) +  geom_polygon(colour='black', fill=NA)
  locator(1)
  </code></pre>
  
  <p>Now <code>grid.locator()</code> as pointed out to me by Dason on talkstats.com (<a href="http://www.talkstats.com/showthread.php/23852-locator-equivalent-in-ggplot2" rel="noreferrer">HERE</a>) can return something.  I just don't know how to use that something to get a map coordinate.</p>
  
  <pre><code>&gt; grid.locator()
  $x
  [1] 286native
  
  $y
  [1] 133native
  </code></pre>
  
  <p>Units didn't seem to help as they are not map coordinates.  Maybe I need some sort of conversion.  </p>
  
  <p>Thank you in advance.</p>
  
  <p><strong>EDIT:  (based on DWin's response)</strong></p>
  
  <p>Dwin's got the right idea but the conversion factor is a little bit off.  Help with that  would be appreciated.  In the example below I have a map with a red dot on it at the coordinates (x = -73 &amp; y = 40.855).  I threw Dwin's response into a function to return the coordinates.  I would expect the results to be the coordinates I put in but they're not.</p>
  
  <p><strong><em>Ideas?</em></strong></p>
  
  <pre><code>require(maps); library(ggplot2); require(grid)
  
  county_df &lt;- map_data('county')  #mappings of counties by state
  ny &lt;- subset(county_df, region=="new york")   #subset just for NYS
  ny$county &lt;- ny$subregion
  
  
  NY &lt;- ggplot(ny, aes(long, lat)) +  
            geom_polygon(aes(group=group), colour='black', fill=NA) +
            coord_map() + geom_point(aes(-73, 40.855, colour="red"))
  NY  
  
  gglocator &lt;- function(object){
      require(maps); require(grid)
      z &lt;- grid.locator("npc")
      y &lt;- sapply(z, function(x) as.numeric(substring(x, 1, nchar(x))))
      locatedX &lt;- min(object$data$long) + y[1]*diff(range(object$data$long))
      locatedy &lt;- min(object$data$lat)  + y[2]*diff(range(object$data$lat))
      return(c(locatedX, locatedy))
  }
  
  #click on the red dot
  gglocator(NY)  #I expect the results to be x = -73 &amp; y = 40.855
  </code></pre>
  
  <p><strong>EDIT 2:  (Going off of Baptise's answer)</strong></p>
  
  <p>We're there</p>
  
  <pre><code>NY &lt;- ggplot(ny, aes(long, lat)) +  
            geom_polygon(aes(group=group), colour='black', fill=NA) +
            coord_map() + geom_point(aes(-73, 40.855, colour="red")) +
            scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0))
  
  
  NY 
  x &lt;- grid.ls()[[1]][grep("panel-", grid.ls()[[1]])] #locate the panel
  seekViewport(x)
  y &lt;-  grid.locator("npc")
  y &lt;- as.numeric(substring(y, 1, nchar(y)-3))
  
  locatedX &lt;- min(NY$data$long) + y[1]*diff(range(NY$data$long))
  locatedy &lt;- min(NY$data$lat) + y[2]*diff(range(NY$data$lat))
  locatedX; locatedy 
  </code></pre>
  
  <p><strong>UPDATE:</strong>  <strong><em>The <code>gglocator</code> function of the <a href="http://cran.r-project.org/web/packages/ggmap/ggmap.pdf" rel="noreferrer">ggmap package</a> now contains this functionality.</em></strong></p>