rggplot2pnggoogle-maps-static-apiggimage

Offline plotting of map coordinates on static maps of Google


History: Extracted raster data from the static Google map png, loaded it on the R device through ggimage.

library (png)
library (ggmap)

rasterArray <- readPNG ("My.png")

x = c (40.702147,40.718217,40.711614)
y = c (-74.012318,-74.015794,-73.998284)

myData <- data.frame (x, y)

print (ggimage (rasterArray, fullpage = TRUE, coord_equal = FALSE) 
    + geom_point (aes (x = x, y = y), data = myData, colour = I("green"), 
      size = I(5), fill = NA))

I did run dput on the rasterArray but the output is of 20 MBs, can't post here.
BTW, this is the URL of that static map:

Question: For plotting "GPS coordinates" on the R device containing the map in pixels, do I need to scale the data.frame?

I saw this page: http://www-personal.umich.edu/~varel/rdatasets/Langren1644.html Do I need to do scaling the way they have shown here?

If yes, then what else other than the man page of scale function do I need to understand to get this done?

Am I barking at the wrong tree?


Solution

  • I think your mistake was the following:

    Here is how you should do it instead, in two steps:

    1. Get the map with get_map() and save it to disk using save()
    2. Plot the data with ggmap()

    First, get the map.

    library (ggmap)
    
    
    # Read map from google maps and save data to file
    
    mapImageData <- get_googlemap(
      c(lon=-74.0087986666667, lat=40.7106593333333), 
      zoom=15
    )
    save(mapImageData, file="savedMap.rda")
    

    Then, in a new session:

    # Start a new session (well, clear the workspace, to be honest)
    
    rm(list=ls())
    
    # Load the saved file
    
    load(file="savedMap.rda")
    
    # Set up some data
    
    myData <- data.frame(
        lat = c (40.702147, 40.718217, 40.711614),
        lon = c (-74.012318, -74.015794, -73.998284)
    )
    
    
    # Plot
    
    ggmap(mapImageData) +
        geom_point(aes(x=lon, y=lat), data=myData, colour="red", size=5)
    

    enter image description here