I'm working with a data set corresponding to the extract below:
# Data sourcing -----------------------------------------------------------
# Download an read US state shapefiles
tmp_shps <- tempfile(); tmp_dir <- tempdir()
download.file("http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_state_20m.zip",
tmp_shps)
unzip(tmp_shps, exdir = tmp_dir)
# Libs
require(rgdal); require(ggplot2)
# Read
us_shps <- readOGR(dsn = tmp_dir, layer = "cb_2014_us_state_20m")
# Prepare data set for ggplot2
us_shps_frt <- fortify(us_shps, region = "NAME")
From within that data set I subset a custom selection of shapefiles, like in the example below:
map_shps <- ggplot(data = us_shps_frt[grep("South", us_shps_frt$id),]) +
geom_polygon(aes(x = long, y = lat, group = group,
fill = id)) +
coord_equal() +
ggtitle("Odd Map") +
theme_map()
Given the odd selection of shapefiles above I would like to fit a background map that would neatly overlay with the maximum extent of the polygons. Initially I tried to achieve this in a following manner:
# Get box for the map
bbox <- make_bbox(lon = long, lat = lat,
data = us_shps_frt[grep("South", us_shps_frt$id),],
f = 0.5)
map_backgr <- get_map(bbox, maptype = "roadmap", zoom = 5,
source = "google", scale = 2, messaging = TRUE)
map_backgr <- ggmap(map_backgr, extent = "normal", maprange = FALSE)
and then generating the map:
map_backgr +
geom_polygon(data = us_shps_frt[grep("South", us_shps_frt$id),],
aes(x = long, y = lat, group = group,
fill = id)) +
coord_equal() +
ggtitle("Odd Map with Background") +
theme_map()
The produced graphic does not fit the polygons well:
I'm interested in adjusting the background map so it matches the polygons exactly. In particular I would like to crop/shorten the background by the red lines marked in the picture below: