2

I'm plotting a map in UTM projection with ggspatial. The map is plotted in the correct projection, but axis labels are still lat-lon degrees. I want to show UTM coordinates on the axes.

Required output: map of Germany with UTM coordinates in kilometers shown on x and y axes

Example code:

require(tidyverse); require(magrittr); require(raster); require(rworldmap); require(ggspatial)

targetcrs = "+proj=utm +datum=WGS84 +no_defs +zone=32 +units=km"
countriesLow %>% subset(ADMIN == 'Germany') %>% spTransform(targetcrs) -> de
de %>% extent %>% as('SpatialPolygons') -> bbox
crs(bbox) <- targetcrs

ggplot() + coord_sf(crs=targetcrs) + layer_spatial(de) + layer_spatial(bbox, fill=NA) -> p
print(p)

Resulting output: map of Germany in UTM projection, but with grid lines and axis labels in lat-lon degrees

You can easily see from the fact that the bounding box is shown as a rectangle with straight sides that the spatial objects are plotted in the intended projection.

When calling ggplot, a warning gets printed into the console:

Coordinate system already present. Adding new coordinate system, which will replace the existing one.

However, when I check the ggplot object, there is no crs information:

> p$coordinates$crs
NULL

The "required output" above was produced with spplot:

spplot(de[,1], sp.layout=list('sp.lines', bbox), scales=list(draw=T))

However, there are other reasons not to use graphics::plot, sp::spplot or other alternatives, unless it's really not possible to fix this seemingly simple issue with ggspatial.

I have tried reading ggspatial documentation which says that plot CRS is set by coord_sf or defaults to the CRS of the first layer_spatial, but this is not what's happening. I have tried playing around and googling for a solution but am at a loss.

A possible approach would be to create custom scales for x and y, with custom labels and breaks, and either do away with gridlines or create them with annotation_spatial_hline() and vline. However I'm hoping that there is an easy fix.

0 Answers0