jarvsh2929
jarvsh2929

Reputation: 23

R: cropping/zooming a map

I am trying to overlay data on top of a map of Canada, however I can't adjust the zoom as I would like. In the map, I want to be able to see the lines for each province anong with its name (so using map("world", "Canada") isn't desirable)

I have tried altering the zoom, but one is too zoomed out and the other is too zoomed in:

qmap(location = "Canada", zoom = 3)

enter image description here

qmap(location = "Canada", zoom = 4)

enter image description here

I have tried researching on how to crop the image but have been unsuccessful

Thank you!

Upvotes: 1

Views: 8378

Answers (1)

ccapizzano
ccapizzano

Reputation: 1616

You can approach this using either the maps and mapsdata packages or continue with ggmap. It just depends on how much detail you want.

library(raster)
library(maps)
library(mapdata)
canada<-getData("GADM",country="CAN",level=1)

plot(canada,xlim=c(-141,-53),ylim=c(40,85),col="gray80",border="gray40",axes=T,las=1)
invisible(text(getSpPPolygonsLabptSlots(canada),labels=as.character(substr(canada$HASC_1,4,5)), 
               cex=0.75, col="black",font=2))

enter image description here

Using ggmap, you can specify a bounding box when grabbing your spatial data. It still overplots some of the area you are interested in (i.e., plots most of the US). Therefore, reapplying the bounding box values to the ggmap function cuts down the viewable area.

library(ggmap)

lats<-c(40,85)
lons<-c(-141,-53)
bb<-make_bbox(lon=lons,lat=lats,f=0.05)
cda<-get_map(bb,zoom=3,maptype="terrain")
ggmap(cda)+xlim(lons)+ylim(lats)+theme_bw()+labs(x="Longitude",y="Latitude")

enter image description here

Upvotes: 3

Related Questions