1

I am attempting to plot several shapefiles on top of a map generated through ggmap. This is working well, however I want to constrain the view area to the shapefile (and not rely on the zoom argument in ggmaps). I've done this by getting the bounding box and passing it as an argument in ggplot's coord_cartesian While this works, I am getting some tearing issues on the edges of the map - most specifically on the western portion. I've tried adjusting the x-y coordinates manually but it seems to only severely distort the picture.

Detroit Shapefile Map

My thoughts are to zoom out slightly to allow the entire shapefile to be plotted in the area, but I can't seem to figure it out. It's also possible I am going about this entirely in the wrong way.

Here's the code I used to generate the map. The shapefile can be downloaded here

library(dplyr)
library(ggmap)
library(rgdal)
library(broom)

# Read in shapefile, project into long-lat
# Create 'tbox' which is a minimum bounding box around the shapefile

tracts <- readOGR(dsn = ".", layer = "CensusTracts2010") %>%
  spTransform("+proj=longlat +ellps=WGS84")
tbox <- bbox(tracts)

# Plot data
tract_plot <- tidy(tracts)

DetroitMap <- qmap("Detroit", zoom = 11)

DetroitMap + geom_polygon(data = tract_plot, aes(x = long, y = lat, group = id), color = "black", fill = NA) +
  coord_cartesian(xlim = c(tbox[1,1], tbox[1,2]),
                  ylim = c(tbox[2,1], tbox[2,2]))
Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62
Gio Circo
  • 332
  • 2
  • 9

1 Answers1

0

I followed your workflow, which resulted in the same problem as you mentioned above. Then I changed the zoom on the qmap option from 11 to 10 and it resulted in a much better picture, although you do lose some of the place names but you can add those in manually yourself with annotate:

DetroitMap <- qmap("Detroit", zoom = 10)

DetroitMap + geom_polygon(data = tract_plot, aes(x = long, y = lat, group = id), color = "black", fill = NA) +
    coord_cartesian(xlim = c(tbox[1,1], tbox[1,2]),
                    ylim = c(tbox[2,1], tbox[2,2]))

enter image description here

Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62