Skip to content

Instantly share code, notes, and snippets.

@obrl-soil
Last active June 20, 2017 05:57
Show Gist options
  • Select an option

  • Save obrl-soil/e781eb4dfaafd96d3a49d41124c41c90 to your computer and use it in GitHub Desktop.

Select an option

Save obrl-soil/e781eb4dfaafd96d3a49d41124c41c90 to your computer and use it in GitHub Desktop.
ggplot with projected coords
library(sp)
library(sf)
library(tidyverse)
library(rosm)
library(ggspatial)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
ncsp <- as(nc, 'Spatial')
# only for sf < 0.5 I think...?
row.names(ncsp) <- sapply(slot(ncsp, "polygons"), function(x) slot(x, "ID"))
ggplot() +
geom_spatial(ncsp,
aes(fill = NAME),
crsto = 32617,
show.legend = FALSE) +
labs(x = 'Easting', y = 'Northing') +
coord_fixed() # or coord_equal()
# this is messy af but to specify tick marks something like the following should work
scale_x_continuous(breaks = seq(round(bbox(spTransform(ncsp, CRSobj = as.CRS(32617)))[1,1], -3),
round(bbox(spTransform(ncsp, CRSobj = as.CRS(32617)))[1,2], -3),
by = 250000)) +
scale_y_continuous(breaks = seq(round(bbox(spTransform(ncsp, CRSobj = as.CRS(32617)))[2,1], -3),
round(bbox(spTransform(ncsp, CRSobj = as.CRS(32617)))[2,2], -3),
by = 100000))
# eww.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment