Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 463
ze miguel Avatar asked Dec 18 '22 06:12

ze miguel


2 Answers

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

like image 143
Edzer Pebesma Avatar answered Apr 30 '23 23:04

Edzer Pebesma


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.

like image 20
Spacedman Avatar answered Apr 30 '23 23:04

Spacedman