ggmap changing size of map
Asked Answered
G

2

9

I would like to create a map that is not perfectly square but rectangular and is the size I dictate.

require(ggmap)
tenmile <- get_map(location = c(lon = -122.486328, lat = 48.862813),
    color = "color",
    source = "google",
    maptype = "roadmap",
    zoom = 12)
tenmile.map <- ggmap(tenmile, 
    extent = "device",
    ylab = "Latitude",
    xlab = "Longitude")+ggtitle("GEOMean for Data from Oct 2013-Nov 2014")
tenmile.map + geom_point(data=pp, aes(x=lon, y=lat, size=geomean), color="red", alpha=0.5) +      
geom_text(data=pp, aes(x=lon, y=lat, label = site), size=3, vjust = 1.25, hjust = -0.1)

I would post pictures of what I get and what I want but I do not have enough reputation points to post images. =-(

Gyrostatic answered 13/1, 2015 at 4:54 Comment(1)
It seems like ggmap doesn't do non-square maps, but maybe you can get a larger map, crop the image and use it as a background. I'm guessing it'll be painful to align the coordinates, maybe here.Innkeeper
W
4

Sandy Muspratt's answer produces a rectangular map, but it gets stretched. To get an unstretched map, ratio must be adjusted to the ratio between spacing of parallels and meridians at the place of the map. That is:

ratio = 1/cos(latitude)

If latitude is given in degrees, that becomes:

ratio = 1/cos(pi*latitude/180)

I give here an example using a map of Barcelona (Barcelona makes a good example to check for stretching because most of our streets form an square grid and deformation becomes easily noticeable).

library(ggmap) library(mapproj) mapbcn <- get_map(location =
  'Barcelona, Catalonia', zoom = 13)

# square map (default) ggmap(mapbcn)

# map cropped by latitude 
ggmap(mapbcn) +                
  coord_fixed(ylim=c(41.36,41.41), 
              ratio=1/cos(pi*41.39/180))

# map cropped by longitude 
ggmap(mapbcn) +    
  coord_fixed(xlim=c(2.14, 2.18), 
              ratio=1/cos(pi*41.39/180))

It must be noted that this way coordinates keep working for the whole map (for example to add points to the map) if the area of the map is small enough not to take in account Earth's curvature - that is, to assume that meridians are parallel in the area shown by the map. It may be inaccurate in a map spanning some hundreds of kilometres and very wrong in a continent-scale map.

Winona answered 26/11, 2017 at 18:52 Comment(0)
P
3

If you want to keep the original limits of the bounding box but simply to change its shape, you can adjust the aspect ratio. If you want to change the limits of the bounding box, then obtain the map as before but set its limits using coord_fixed() (or coord_cartesian()). Or you can adjust both the aspect ratio and the limits of the bounding box.

tenmile <- get_map(location = c(lon = -122.486328, lat = 48.862813),
  color = "color",
  source = "google",
  maptype = "roadmap",
  zoom = 12)
tenmile.map <- ggmap(tenmile, 
  ylab = "Latitude",
  xlab = "Longitude")+ggtitle("GEOMean for Data from Oct 2013-Nov 2014") +
  coord_fixed(xlim = c(-122.55, -122.40), ratio = 2/1)
Phonologist answered 15/1, 2015 at 4:17 Comment(2)
Thanks for the Code Sandy but I can't get it to work.Gyrostatic
Sorry, missed your comment. You need to explain what you expect the map to look like. I get a map that's not square.Phonologist

© 2022 - 2024 — McMap. All rights reserved.