Skip to contents

This is a wrapper around ggplot2::coord_sf() that automatically calculates coordinate limits based on the data and/or any additional locations. The bounding box will be calculated to encompass all of the included locations.

Usage

coord_sf_zoom(include = NULL, include_data = TRUE, ...)

Arguments

include

Vector of feature names that should be shown on the map.

include_data

Scalar logical, if true then all features with data are also included.

...

Additional arguments passed to ggplot2::coord_sf().

Value

A zoom specification that can be added to a ggplot object with ggplot2::%+%.

Details

This should be added to the plot after the call to one of the ggautomap geoms. It will copy the data and feature_type from the first such layer in the plot. If there is no such layer, it will attempt to guess the feature type and use the data and location mapping found at the top level ggplot() call.

Examples

library(ggplot2)

# zoom in on locations that have data:
cartographer::nc_type_example_2 |>
  ggplot(aes(location = county)) +
  geom_boundaries(feature_type = "sf.nc") +
  geom_choropleth() +
  coord_sf_zoom()


# or just zoom in on specific locations regardless of the data:
cartographer::nc_type_example_2 |>
  ggplot(aes(location = county)) +
  geom_boundaries(feature_type = "sf.nc") +
  coord_sf_zoom(include = c("Rowan", "Polk"), include_data = FALSE)