1

I'm trying to map an area by using ggmap. The problem is that ggmap only plots a square map. What I'd like to do is to expand just a little bit the map, so the map would fit perfectly. Here is my code. You can download the shapefile data here.

AMB_Contour <- readOGR(dsn = '.', layer='AMB_Contour')
myprojection_latlong <- "+proj=longlat +zone=31 +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
AMB_Contour <-  spTransform(AMB_Contour, myprojection_latlong)
tract_plot <- tidy(AMB_Contour)

CenterOfMap <- geocode("41.396108,2.059348")
Ciutat_NoLabels <- get_googlemap(c(lon=CenterOfMap$lon, lat=CenterOfMap$lat),zoom = 11, maptype = "terrain", source = "google", color="bw", style=c(feature="all", element="labels",visibility="off")) 
ggmap(Ciutat_NoLabels)

CityMap_NoLabels <- ggmap(Ciutat_NoLabels, extent = "device")
print(CityMap_NoLabels)

CityMapWithRoutesAndStops <- CityMap_NoLabels + geom_polygon(aes(x=long, y=lat, group=group), fill=NA, size=1.5, color='black', data=tract_plot, alpha=1)
print(CityMapWithRoutesAndStops)

In the following image the eastern part of my map doesn't fit completely. I've also tried adjusting the x-y coordinates manually but it doesn't work. I don't want to change the zoom argument because for me it's perfect, the only problem is the eastern part. Finally, I want that maptype (terrain).

image

0

Since I don't have an API key I'll use get_stamenmaps for the example; I'm using the library sf here since it is meant to succeed sp (the author of both libraries).

st_bbox returns a vector in the order xmin, ymin, xmax, ymax which you may pass to the get_stamenmap call, furthermore, as a vector, you may add another vector of values, such as the one you see below; in this case it is half a degree (0.5) because the map is EPSG 4326.

library(sf)
library(ggmap)
library(dplyr)

amb_contour = read_sf("/home/elio/Downloads/AMB_Contour/AMB_Contour.shp")
bounds = st_bbox(amb_contour) + c(-.05,-.05,.05,.05)
mapis = get_stamenmap(bbox = as.numeric(bounds),
                      maptype = "terrain-background", zoom = 8)

ggmap(mapis) +
  geom_sf(inherit.aes = F, data = amb_contour)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.