Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggmap changing size of map

Tags:

r

ggmap

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. =-(

like image 539
Danielle Love Avatar asked Jan 13 '15 04:01

Danielle Love


2 Answers

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.

like image 136
Pere Avatar answered Oct 23 '22 05:10

Pere


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)
like image 27
Sandy Muspratt Avatar answered Oct 23 '22 05:10

Sandy Muspratt