1

I'm trying to create a map using the qmap (part of ggmap). Here's an example piece of code to illustrate by question. It's kind of silly, but it's cleaner than using my own data.

install.packages("ggmap")

library(ggmap)

qmap("Capitol Building, Washington DC", zoom = 15)

So here's my question: I'd like to zoom in a good bit (zoom=15) on the area around the Capitol Building, to get lots of street detail. But I also want to include the Washington Monument in my map. To do this, I'd like to extend the western part of the map and make it rectangular.

Does anybody know how to do that? Any insight would be much appreciated. Thanks for your patience with a beginner.

jlhoward
  • 58,004
  • 7
  • 97
  • 140

1 Answers1

2

Like this?

library(ggmap)
cap  <- geocode("Capitol Building, Washington DC")
wash <- geocode("Washington Monument, Washington DC")
loc  <- unlist((cap+wash)/2)
ggmap(get_map(location=loc,zoom=15))+coord_fixed(ylim=loc[2]+.005*c(-1,+1))

So this pulls in a map based on coords midway between the Capitol Building and the Washington Monument, then trims it by setting the ylim.

The reason for unlist(...) is that geocode(...) returns a data frame and get_map(...) wants a numeric vector.

EDIT Response to OP's comment.

coord_fixed(...) forces an aspect ratio of 1:1, meaning that 1° of latitude is the same length as 1° of longitude. To get back the original aspect ratio from the map, use coord_map(...).

ggmap(get_map(location=loc,zoom=15))+coord_map(ylim=loc[2]+.005*c(-1,+1))

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Thanks for your quick and concise reply. I notice the map's a little horizontally stretched; do you know a way around that? Also, is there a way to pull in the western portion of the map without re-centering it? I guess I'm asking if there's another way to specify the initial dimensions and zoom of the map, other than using the location=loc,zoom=15 stuff. – ConfusedEconometricsUndergrad Oct 11 '14 at 14:49
  • Fantastic, that makes everything clear. Thanks again for your help and patience. – ConfusedEconometricsUndergrad Oct 12 '14 at 04:03