A ggplot2::coord_sf() that limits a map to a bounding box, e.g. the
area of a CensusMapper map's view, while the data can extend beyond it. The
box can be in any CRS: it is turned into the map's CRS. With
bbox = NULL the map shows all its data, as with coord_sf(),
so that cropping is switched off by setting the box to NULL.
geom_cm_context() covers the area shown.
Arguments
- bbox
The area to show: a bounding box (
sf::st_bbox()), or sf data or an sfc whose bounding box to show, or a numeric vectorc(xmin, ymin, xmax, ymax). A box without a CRS, and a numeric vector, are taken to be in the CRS of the map's data (of the plot, or of its first sf layer).NULLto show all data.- crs
The CRS of the map. By default that of the map's data.
- datum
The CRS of the graticule; by default none.
- ...
Other arguments passed on to
ggplot2::coord_sf(), e.g.expand.
Examples
if (FALSE) { # \dontrun{
library(ggplot2)
census_data <- get_census("CA21", regions = list(CMA = "59933"),
vectors = c(population = "v_CA21_1"),
level = "CT", geo_format = "sf")
view_bbox <- sf::st_bbox(c(xmin = -123.19, ymin = 49.24, xmax = -123.05, ymax = 49.30),
crs = 4326)
ggplot(census_data) +
geom_sf(aes(fill = population / `Shape Area`), colour = NA) +
geom_cm_context() +
coord_cm_bbox(view_bbox)
} # }
