Skip to content

Instantly share code, notes, and snippets.

@michaeldorman
Created May 7, 2020 16:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeldorman/b79a36bd376bce08f425d1aba6e40450 to your computer and use it in GitHub Desktop.
Save michaeldorman/b79a36bd376bce08f425d1aba6e40450 to your computer and use it in GitHub Desktop.
library(sf)
library(ggplot2)
# Data
nc = st_read(system.file("shape/nc.shp", package="sf"))
map = nc
dat = nc[5, ]
# Plot 1
ggplot() +
geom_sf(data = map) +
geom_sf(data = dat) +
coord_sf(
xlim = st_bbox(dat)[c(1,3)],
ylim = st_bbox(dat)[c(2,4)]
)
coord_sf_lim = function(x, ...) {
coord_sf(
xlim = st_bbox(x)[c(1,3)],
ylim = st_bbox(x)[c(2,4)],
...
)
}
# Plot 2
ggplot() +
geom_sf(data = map) +
geom_sf(data = dat) +
coord_sf_lim(dat)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment