0

I'm a map newbie in R and am at my wits end trying to figure this out. I used ggmap() to create a map of the U.S. I then overlaid some cities with their latitude and longitudes using geom_point().

When the user does a hover or click, I am able to retrieve the coordinates - however, these coordinates are x-y, that range from 0 to 1.

I'm trying to ascertain that city was clicked by computing the distance between the point of click/hover (x-y) to the city.

How do I covert the latitude / longitude data into x-y coordinates?

Any help is greatly appreciated!

Here is the code: Inside the renderPlot() function, I have the following running:

usa.map = get_map(location = 'United States',zoom = 4, color = 'bw')
g <- ggmap(usa.map) + 
geom_point(data = annual_data, mapping = aes(x=LONGITUDE, y=LATITUDE, colour = delay.class), size=3) + 
scale_colour_gradient(low = "white", high = "red")

In the renderPrint() function, I'm catching a hover event:

output$hover_info <- renderPrint({
if(!is.null(input$plot_hover)){
hover=input$plot_hover
}
})

The input$plot_hover object has fields X and Y, that range from 0 to 1 i.e. in the plotting process, the latitude and longitude values have been rasterized (I'm not sure, but that's what I think).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Karthik B
  • 3
  • 3
  • 1
    We can't provide a solution without a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – jdobres Oct 02 '16 at 20:56
  • You can get the bounding box of a map with `attr(usa.map, "bb")`. Should be possible to use that - not sure what projection googlemap uses. – Richard Telford Oct 02 '16 at 22:42
  • I tried it - it gives the bounding box in terms of latitude and longitude. I'm trying to get the xy coordinates that range from 0 to 1, once the map is plotted by ggmap(). – Karthik B Oct 02 '16 at 22:49
  • maybe related to [this](http://stackoverflow.com/questions/38806503/shiny-click-brush-not-working-with-non-cartesian-coordinates/)? – donlelek Oct 02 '16 at 23:20

0 Answers0