plotting a sf object with geom_sf() with any projection other than lat-long
Asked Answered
R

2

5

I am trying to plot a polygon with geom_sf() in any projection other than lat-long.

I am using the example found in the manual pages for geom_sf() Importing the dataset:

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

transforming from latlong into epsg:3857

nc_3857 <- sf::st_transform(nc, "+init=epsg:3857")

Finally plot with ggplot2 defining the crs of the plot:

ggplot() +
 geom_sf(data = nc_3857, colour = "red", fill = NA) +
 coord_sf(crs=st_crs(3857))

I keep getting a map in wgs84 (i.e. epsg:4326) with lat-long axes. I want to have the axes in meters, so I need ggplot to plot the projected polygon. What am I doing wrong?

Respondence answered 3/7, 2017 at 14:6 Comment(0)
H
12

See also https://github.com/tidyverse/ggplot2/issues/2200 and try

ggplot() +  geom_sf(data = nc_3857, colour = "red", fill = NA) +
   coord_sf(datum=st_crs(3857))

which gives

enter image description here

Heelpiece answered 26/7, 2017 at 20:57 Comment(0)
A
2

It is plotting it in the requested projection, its just overlaying a lat-long graticule.

If you try a similar thing with Norway, for example, being close to the north pole you can see that the display X-Y coordinates are those of the transformation but the overlaying graticule is lat-long. This is a map of Norway which is in epsg 3035 (conical) coordinates:

enter image description here

So it is plotting the projected polygon. If the lat-long lines here were a grid then it would have been plotting the coordinates back in lat-long projection.

The only mention of graticules in the docs is an arg to coord_sf:

datum: CRS that provides datum to use when generating graticules

which doesn't really say much.

You just want a cartesian coordinate system? Oh lets try:

> ggplot() +  geom_sf(data = rp, colour = "red", fill = NA) + coord_cartesian()
Error: geom_sf() must be used with coord_sf()

Check the ggplot2 issues for alternate graticules with geom_sf, and add an issue if there's nothing there.

Ambrose answered 3/7, 2017 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.