Offline plotting of map coordinates on static maps of Google
ggimage, ggplot2, google-maps-static-api, png, r
Solution
I think your mistake was the following:
- Trying to plot geographic data on an image, where that image doesn't have any awareness of the map coordinates
- Possibly transposing your latitude and longitudes in the data frame
Here is how you should do it instead, in two steps:
- Get the map with `get_map()` and save it to disk using `save()`
- 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)
Problem
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?