rgistmap

How to create tmap labels along lines with rotated crs?


In the following example, aligning the labels to the lines works in the default crs, but rotating the viewport caused inconsistent results. How could it be modified to work with the rotated viewport?

library(tmap) #4.2.0.9000
library(sf)

tmap_options_reset()
tmap_mode("plot") # Use "view" for interactive map

angle <- -50
crs_string <- glue("+proj=omerc +lat_0=37.773 +lonc=-122.4187",
" +alpha=0 +datum=NAD83 +units=us-ft +no_defs +gamma=",angle)

#Define the 1000ft box (500ft from center)
zoom_bbox <- st_bbox(c(xmin = -500, xmax = 500, ymin = -500, ymax = 500), 
                     crs = crs_string)

base_url <- "https://data.sfgov.org/resource/3psu-pn9h.geojson"
query <- "$where=street IN('MISSION', 'VAN NESS', 'SOUTH VAN NESS')"
full_url <- URLencode(paste0(base_url, "?", query))

streets <- st_read(full_url)

#Transform to your custom rotated CRS
streets_rot <- st_transform(streets, crs_string)

#No rotation: Label is aligned
tm_shape(streets, bbox = zoom_bbox)+
  tm_sf(lty = "solid")+
  tm_labels("street", bgcol = "white", along.lines = TRUE, offset = 2)

#rotated viewport: label alignment is inconsistent
tm_shape(streets_rot, bbox = zoom_bbox, crs = crs_string)+
  tm_sf(lty = "solid")+
  tm_labels("street", bgcol = "white", along.lines = TRUE, offset = 2)

desired and bad output

Here's another example using tmap built-in data, which doesn't have this problem

library(tmap) #4.2.0.9000
library(sf)
tmap_mode("plot") # Use "view" for interactive map

CA_rivers <- filter(World_rivers, name %in% c('San Joaquin', 'Sacramento'))

angle <- -50
crs_string <- glue("+proj=omerc +lat_0=38.8 +lonc=-120.7",
" +alpha=0 +datum=NAD83 +units=us-ft +no_defs +gamma=", angle)

#No rotation: Label is aligned
tm_shape(CA_rivers)+
  tm_sf(lty = "solid")+
  tm_labels("name",  along.lines = TRUE)

#rotated viewport: Label is still aligned
tm_shape(CA_rivers, crs = crs_string)+
  tm_sf(lty = "solid")+
  tm_labels("name",  along.lines = TRUE)

example with tmap built-in data


Solution

  • Problem caused due to use of development version 4.2.0.9000. Works OK with 4.2

    https://github.com/r-tmap/tmap/issues/1219