Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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