Multiple panned maps of various sections of the city with ggmap

795 views Asked by At

I can achive Plotting multiple maps with ggmap which is also possible with faceting as described in Kale & Wickham (2013) R Journal paper. But I would like to plot a multiple series of maps that pan around particular, zoomed areas of the city. This is sort of achievable if I look at the city coordinates obtained with geocode() function and roughly figure out what I need to subtract or add from longitude/latitude on each side of pan the view. Such solution is far from ideal. Let me illustrate with an example (note: multiplot function used from Cookbook For R).

library(ggmap)
library(RgoogleMaps)

#getting Bristol lat/long
BRS <- geocode("Bristol, UK")

#get the first (central) map from the coordinates
BristolMapCenter <- ggmap(get_map(c(lon=BRS$lon, lat=BRS$lat), zoom = 15))

#get the second map panned to the right by adding approx. 0.015 to longitude
BristolMapRight <- ggmap(get_map(c(lon=BRS$lon+0.015, lat=BRS$lat),zoom = 15))

#multiplot function
multiplot(BristolMapCenter, BristolMapRight, cols=2)

enter image description here

As you can see this is far from ideal as there is overall (I don't want overlap, I want "lined up continuation"), if not to say clunky, especially if I want to get a larger panning of the surrounding areas (lets say 9-12 maps), do it for multiple cities, plot some data on the top of it, and still have enough time in my life to grab a pint on the sun. So I wonder if there is any fast, sleek and automatic way to get those panned maps based on specific central coordinates?

1

There are 1 answers

0
Gregor Thomas On BEST ANSWER

Starting with your code:

library(ggmap)
library(RgoogleMaps)

#getting Bristol lat/long
BRS <- geocode("Bristol, UK")

#get the first (central) map from the coordinates
BristolMapCenter <- ggmap(get_map(c(lon=BRS$lon, lat=BRS$lat), zoom = 15))

we can extract the absolute ranges covered by the map at this zoom level:

z15width = sapply(BristolMapCenter$data, function(x) abs(diff(range(x))))

And then add multiples of this to your BRS coordinates.

BristolRight = ggmap(get_map(c(lon = BRS$lon + z15width["lon"],
                               lat = BRS$lat), zoom = 15))

These should line up nicely.

multiplot(BristolMapCenter, BristolRight, cols = 2)

enter image description here

You can move in any orthogonal direction by integer multiples of z15width, and things should continue to line up. And of course, you get a different width at a different zoom. You could write a script to calculate the width for many different zoom values and store that somewhere and reference it later.