Search code examples
rggplot2r-sfggspatial

Hide area outside of area of interest in ggspatial


I made a map using ggplot2, ggspatial and sf. I used ggspatial so that I could download open source base maps without relying on Google and needing an API key. The ggspatial annotation_map_tile call downloads a set of tiles that provide the base map. I am trying to white out the map outside of the state I am interested in so that the base map only shows within the state.

Here's my code. The dput for the sf object is at the bottom. When you run it, you see a map of New Mexico with a black line outlining the state. I'd like to make the map white outside of the state.

library(ggplot)
library(ggspatial)
library(sf)
ggplot() + 
  annotation_map_tile(zoom = 7, type = "hikebike") +
  geom_sf(data = sf, fill = NA, color = "black", lwd = 2)

I tried two things. First, I though about somehow inverting the fill argument in geom_sf, but don't think there's a way to do that. My other attempt was to include all states in an extra geom_sf call and fill those white. That might have worked but it required downloading over 4,000 map tiles, which I'd like to avoid, even though map tiles can be saved using the cachedir argument.

sf <- structure(list(GEOID = "35", geometry = structure(list(structure(list(
  list(structure(c(-109.050044, -109.050173, -109.049843, -109.049813, 
                   -109.049112, -109.049195, -109.048763, -109.049106, -109.048769, 
                   -109.04859, -109.048599, -109.048731, -109.048296, -109.048286, 
                   -109.047612, -109.047653, -109.047653, -109.047645, -109.047638, 
                   -109.047117, -109.04748, -109.047453, -109.046905, -109.047013, 
                   -109.047117, -109.047116, -109.047237, -109.04747, -109.046827, 
                   -109.046909, -109.047045, -109.04687, -109.046564, -109.047298, 
                   -109.046662, -109.047145, -109.046426, -109.047006, -109.046182, 
                   -109.046182, -109.046156, -109.046086, -109.045363, -109.046104, 
                   -109.045624, -109.046072, -109.045851, -109.046084, -109.046796, 
                   -109.046481, -109.046509, -109.046296, -109.046295, -109.046024, 
                   -109.046055, -109.046054, -109.046011, -109.045973, -109.045729, 
                   -109.046183, -109.045431, -109.045433, -109.045407, -109.045272, 
                   -109.045244, -109.045223, -108.958868, -108.954404, -108.620309, 
                   -108.619689, -108.320721, -108.320464, -108.2884, -108.288086, 
                   -108.250635, -108.249358, -108.000623, -107.420913, -106.877292, 
                   -106.869796, -106.750591, -106.675626, -106.661344, -106.628733, 
                   -106.628652, -106.617125, -106.617159, -106.500589, -106.343139, 
                   -106.293279, -106.248675, -106.247705, -106.201469, -106.006634, 
                   -105.997472, -105.996159, -105.716471, -105.66472, -105.62747, 
                   -105.533922, -105.512485, -105.508836, -105.465182, -105.447255, 
                   -105.442459, -105.41931, -105.251296, -105.220613, -105.1208, 
                   -105.029228, -105.000554, -104.73212, -104.732031, -104.645029, 
                   -104.625545, -104.624556, -104.519257, -104.338833, -104.250536, 
                   -104.007855, -103.734364, -103.733247, -103.155922, -103.002199, 
                   -103.002247, -103.001964, -103.002198, -103.002518, -103.002252, 
                   -103.002188, -103.002565, -103.002434, -103.041924, -103.041745, 
                   -103.041674, -103.040824, -103.041305, -103.042186, -103.041716, 
                   -103.041917, -103.041146, -103.041554, -103.042366, -103.042775, 
                   -103.042497, -103.042377, -103.042366, -103.042339, -103.042395, 
                   -103.042568, -103.042711, -103.0426, -103.04252, -103.043261, 
                   -103.042642, -103.042521, -103.042781, -103.04277, -103.042769, 
                   -103.042827, -103.043286, -103.043072, -103.043594, -103.043589, 
                   -103.043588, -103.043582, -103.043538, -103.043583, -103.043611, 
                   -103.043585, -103.043613, -103.043613, -103.043614, -103.04363, 
                   -103.043693, -103.043919, -103.043944, -103.043946, -103.043979, 
                   -103.043936, -103.043719, -103.043644, -103.043569, -103.043516, 
                   -103.043686, -103.043744, -103.043767, -103.043721, -103.043771, 
                   -103.043746, -103.043555, -103.043531, -103.043617, -103.04395, 
                   -103.044893, -103.045698, -103.045644, -103.046907, -103.047346, 
                   -103.049096, -103.049608, -103.050148, -103.050532, -103.051087, 
                   -103.051535, -103.051363, -103.051664, -103.05261, -103.056655, 
                   -103.057487, -103.057856, -103.059242, -103.05972, -103.060103, 
                   -103.063905, -103.06398, -103.064452, -103.064625, -103.064679, 
                   -103.064657, -103.064569, -103.064701, -103.064862, -103.064807, 
                   -103.064916, -103.064889, -103.064672, -103.064699, -103.064711, 
                   -103.064698, -103.064807, -103.064827, -103.064799, -103.064798, 
                   -103.064864, -103.064633, -103.064815, -103.064761, -103.064788, 
                   -103.064761, -103.064696, -103.064422, -103.064348, -103.064344, 
                   -103.064423, -103.085876, -103.088698, -103.215641, -103.267633, 
                   -103.267708, -103.270383, -103.278521, -103.326501, -103.748317, 
                   -103.875476, -103.980179, -104.024521, -104.531756, -104.531937, 
                   -104.640918, -104.643526, -104.918272, -105.077046, -105.078605, 
                   -105.11804, -105.131377, -105.132916, -105.14824, -105.15031, 
                   -105.153994, -105.390396, -105.427049, -105.428582, -105.429281, 
                   -105.731362, -105.750527, -105.854061, -105.886159, -105.9006, 
                   -105.998003, -106.125534, -106.18184, -106.200699, -106.205915, 
                   -106.313307, -106.376861, -106.394298, -106.411075, -106.565142, 
                   -106.566056, -106.587972, -106.595333, -106.598639, -106.599096, 
                   -106.618486, -106.619448, -106.623568, -106.631182, -106.636492, 
                   -106.639529, -106.638186, -106.630114, -106.626466, -106.623216, 
                   -106.621873, -106.619569, -106.618745, -106.619371, -106.620454, 
                   -106.624299, -106.625535, -106.625123, -106.622819, -106.617708, 
                   -106.614702, -106.616136, -106.623659, -106.622377, -106.622117, 
                   -106.622529, -106.625322, -106.629747, -106.628663, -106.623933, 
                   -106.611846, -106.614346, -106.623445, -106.625947, -106.633668, 
                   -106.64084, -106.645479, -106.645646, -106.645296, -106.6429, 
                   -106.638154, -106.633927, -106.630692, -106.629197, -106.630799, 
                   -106.634873, -106.63588, -106.635926, -106.627808, -106.625763, 
                   -106.621857, -106.614637, -106.605845, -106.605245, -106.602045, 
                   -106.601945, -106.605267, -106.602727, -106.593826, -106.589045, 
                   -106.588045, -106.582144, -106.581344, -106.577244, -106.570944, 
                   -106.566844, -106.563444, -106.562945, -106.558444, -106.547144, 
                   -106.545344, -106.544714, -106.542144, -106.542097, -106.535843, 
                   -106.535343, -106.535154, -106.534743, -106.533043, -106.533, 
                   -106.53248, -106.530515, -106.527943, -106.527738, -106.527623, 
                   -106.527997, -106.528543, -106.528543, -106.750547, -106.750547, 
                   -106.993544, -106.998235, -107.00056, -107.00056, -107.296824, 
                   -107.422246, -107.422495, -108.208394, -108.208087, -108.208521, 
                   -108.208572, -108.208573, -108.707657, -108.788711, -108.851105, 
                   -108.861028, -109.050044, 31.332502, 31.480004, 31.499515, 
                   31.499528, 31.636598, 31.796551, 31.810776, 31.843715, 31.861383, 
                   31.870791, 32.013651, 32.028174, 32.084093, 32.089114, 32.426377, 
                   32.681379, 32.686327, 32.689988, 32.693439, 32.77757, 33.06842, 
                   33.069427, 33.091931, 33.092917, 33.137559, 33.137995, 33.208965, 
                   33.250063, 33.365272, 33.36557, 33.36928, 33.372654, 33.37506, 
                   33.409783, 33.625055, 33.74001, 33.875052, 34.00005, 34.522393, 
                   34.522553, 34.579291, 34.771016, 34.785406, 34.799981, 34.814226, 
                   34.828566, 34.959718, 35.250025, 35.363606, 35.546326, 35.54644, 
                   35.614251, 35.616517, 35.8798, 35.888721, 35.92586, 35.925896, 
                   36.002338, 36.117028, 36.181751, 36.500001, 36.874589, 36.874998, 
                   36.968871, 36.969489, 36.999084, 36.998913, 36.998906, 36.999287, 
                   36.999249, 36.99951, 36.999499, 36.99952, 36.999555, 36.999561, 
                   36.999015, 37.000001, 37.000005, 37.000139, 36.992426, 36.992461, 
                   36.993123, 36.993243, 36.993161, 36.993175, 36.993004, 36.992967, 
                   36.993768, 36.99423, 36.99389, 36.994288, 36.994266, 36.994122, 
                   36.995343, 36.995417, 36.995418, 36.995849, 36.995874, 36.995679, 
                   36.995875, 36.995777, 36.995895, 36.995991, 36.996017, 36.995994, 
                   36.995856, 36.995605, 36.995169, 36.995428, 36.992729, 36.993264, 
                   36.993484, 36.993447, 36.993378, 36.993599, 36.994377, 36.993766, 
                   36.993535, 36.994644, 36.996239, 36.998041, 36.998016, 37.000232, 
                   37.000104, 36.911587, 36.909573, 36.719427, 36.675186, 36.61718, 
                   36.602716, 36.526588, 36.500397, 36.500439, 36.318267, 36.317534, 
                   36.055231, 35.837694, 35.825217, 35.814072, 35.796441, 35.791583, 
                   35.622487, 35.250056, 35.241237, 35.211862, 35.183149, 35.182786, 
                   35.181922, 35.178573, 35.159318, 35.144735, 35.142766, 35.135596, 
                   35.125058, 35.109913, 34.899546, 34.850243, 34.792224, 34.747361, 
                   34.671188, 34.653099, 34.619782, 34.46266, 34.459774, 34.459662, 
                   34.455657, 34.405463, 34.400678, 34.397105, 34.393716, 34.390442, 
                   34.388679, 34.384969, 34.38469, 34.383578, 34.380916, 34.37966, 
                   34.379555, 34.312764, 34.302585, 34.289441, 34.256903, 34.087947, 
                   34.079382, 34.063078, 34.049986, 34.043545, 34.04232, 34.041538, 
                   34.037294, 34.032714, 34.018014, 34.003633, 33.974629, 33.945617, 
                   33.906299, 33.901537, 33.8503, 33.824675, 33.74627, 33.737766, 
                   33.701971, 33.672408, 33.658186, 33.650487, 33.64195, 33.629489, 
                   33.570599, 33.388438, 33.329477, 33.315234, 33.260371, 33.256262, 
                   33.219225, 33.042055, 33.038693, 33.01029, 32.999899, 32.964373, 
                   32.959097, 32.900014, 32.879355, 32.868346, 32.857696, 32.85726, 
                   32.849359, 32.82847, 32.827531, 32.784593, 32.783602, 32.777303, 
                   32.726628, 32.708694, 32.690761, 32.682647, 32.64642, 32.624537, 
                   32.601863, 32.600397, 32.587983, 32.522193, 32.145006, 32.123041, 
                   32.087051, 32.000518, 32.000465, 32.000453, 32.000513, 32.000475, 
                   32.000324, 32.000326, 32.000419, 32.00037, 32.000198, 32.000554, 
                   32.000125, 32.00001, 32.000117, 32.000311, 32.000396, 32.000443, 
                   32.000496, 32.000579, 32.000533, 32.000485, 32.000524, 32.000518, 
                   32.000485, 32.000497, 32.000497, 32.000607, 32.000638, 32.0006, 
                   32.000577, 32.001564, 32.002206, 32.00235, 32.00197, 32.0021, 
                   32.002328, 32.002533, 32.00205, 32.001785, 32.001762, 32.001512, 
                   32.001172, 32.001484, 32.001334, 32.000736, 32.000759, 32.000749, 
                   32.000778, 32.000754, 32.000731, 32.000495, 31.994733, 31.990999, 
                   31.989809, 31.985719, 31.980348, 31.97682, 31.971258, 31.97069, 
                   31.97291, 31.972933, 31.971578, 31.966955, 31.964777, 31.963403, 
                   31.961054, 31.957476, 31.954531, 31.952891, 31.956008, 31.956, 
                   31.948439, 31.94551, 31.940863, 31.936621, 31.934863, 31.930053, 
                   31.92657, 31.923614, 31.925335, 31.920003, 31.918003, 31.914034, 
                   31.912227, 31.90979, 31.904598, 31.89867, 31.895649, 31.894859, 
                   31.892933, 31.891663, 31.889184, 31.886411, 31.883717, 31.879697, 
                   31.874478, 31.871514, 31.866235, 31.860593, 31.856276, 31.852854, 
                   31.84649, 31.846305, 31.845905, 31.844405, 31.839605, 31.827912, 
                   31.825024, 31.824901, 31.822706, 31.822106, 31.815506, 31.813906, 
                   31.810406, 31.810206, 31.813306, 31.812606, 31.811104, 31.810406, 
                   31.807305, 31.805007, 31.804287, 31.802107, 31.802146, 31.798607, 
                   31.797507, 31.797089, 31.796107, 31.791907, 31.791829, 31.791914, 
                   31.792103, 31.790507, 31.789761, 31.789119, 31.786945, 31.784407, 
                   31.783907, 31.783706, 31.783898, 31.783689, 31.783671, 31.783679, 
                   31.783513, 31.783762, 31.783599, 31.783599, 31.783599, 31.613489, 
                   31.499798, 31.499742, 31.333395, 31.333191, 31.332365, 31.332301, 
                   31.332315, 31.332502), .Dim = c(401L, 2L)))), class = c("XY", 
                                                                           "MULTIPOLYGON", "sfg"))), class = c("sfc_MULTIPOLYGON", "sfc"
                                                                           ), precision = 0, bbox = structure(c(xmin = -109.050173, ymin = 31.332301, 
                                                                                                                xmax = -103.001964, ymax = 37.000232), class = "bbox"), crs = structure(list(
                                                                                                                  input = "EPSG:4326", wkt = "GEOGCRS[\"WGS 84\",\n    DATUM[\"World Geodetic System 1984\",\n        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            LENGTHUNIT[\"metre\",1]]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433]],\n    CS[ellipsoidal,2],\n        AXIS[\"geodetic latitude (Lat)\",north,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        AXIS[\"geodetic longitude (Lon)\",east,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n    USAGE[\n        SCOPE[\"Horizontal component of 3D system.\"],\n        AREA[\"World.\"],\n        BBOX[-90,-180,90,180]],\n    ID[\"EPSG\",4326]]"), class = "crs"), n_empty = 0L)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            -1L), sf_column = "geometry", agr = structure(c(GEOID = NA_integer_), .Label = c("constant", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "aggregate", "identity"), class = "factor"), class = c("sf", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    "tbl_df", "tbl", "data.frame"))

Solution

  • You can create a big white mask around NM state, and overlay it over the annotation. It will clip the outside area.

    You can get the NM state outline via {tigris} - highly recommended for any kind US vector data - and create the mask via a combination of sf::st_buffer() and sf::st_difference().

    As the mask reaches, by necessity, outside of the plot area you will need to set the x and y limits of your plot by including a coord_sf() call.

    Consider this code:

    nm <- tigris::states() %>% 
      filter(STUSPS == "NM")
    
    big_white <- st_buffer(nm, units::as_units(150, "km")) %>% 
      st_difference(nm)
    
    
    ggplot() + 
      annotation_map_tile(zoom = 7, type = "hikebike") +
      geom_sf(data = sf, fill = NA, color = "black", lwd = 2) +
      geom_sf(data = big_white, fill = "white", color = NA) +
      coord_sf(xlim = c(-102.5, -109), ylim = c(31.5, 37), crs = 4326)
    

    enter image description here