xmod and ymod parameters of tm_text do not preserve along.lines
Hi! I was creating some maps with lines and text and I found this weird behaviour. The following should be a reprex of the problem. First I create some data with lines and text
# packages
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(tmap)
# create some fake data
my_sfc <- st_sfc(
st_linestring(matrix(c(0, -2, 0, -1, 0, 0), ncol = 2, byrow = TRUE)),
st_linestring(matrix(c(0, 2, 0, 1, 0, 0), ncol = 2, byrow = TRUE)),
st_linestring(matrix(c(-2, 0, -1, 0, 0, 0), ncol = 2, byrow = TRUE)),
st_linestring(matrix(c(2, 0, 1, 0, 0, 0), ncol = 2, byrow = TRUE))
)
my_sf <- st_sf(
data.frame(my_text = c("1", "2", "3", "4")),
geometry = my_sfc
)
Then I create the map and everything is fine.
tm_shape(my_sf) +
tm_lines() +
tm_text("my_text", size = 2, along.lines = TRUE)
#> Warning: Currect projection of shape my_sf unknown. Long-lat (WGS84) is assumed.

The problems occur when I try to modify xmod and ymod to move the labels since, in this case, they do not preserve the along.lines = TRUE option.
tm_shape(my_sf) +
tm_lines() +
tm_text(
"my_text",
size = 2,
along.lines = TRUE,
xmod = c(1, 1, 0, 0),
ymod = c(0, 0, 1, 1)
)
#> Warning: Currect projection of shape my_sf unknown. Long-lat (WGS84) is assumed.

Created on 2019-11-26 by the reprex package (v0.3.0)
Is this a bug? If not, is it possible to solve the problem? Thanks
Not really a bug, but a result of the current implementation. For text post-processing, namely auto.placement, remove.overlap, along.lines and overwrite.lines, grobs are used for calculation (e.g. of the rotation angles): https://github.com/mtennekes/tmap/blob/master/R/plot_map.R#L94-L247. I am not happy with this implementation.
It requires some effort to re-implement.
This problem is fixed in the upcoming tmap v4:
library(sf)
library(tmap)
# create some fake data
my_sfc <- st_sfc(
st_linestring(matrix(c(0, -2, 0, -1, 0, 0), ncol = 2, byrow = TRUE)),
st_linestring(matrix(c(0, 2, 0, 1, 0, 0), ncol = 2, byrow = TRUE)),
st_linestring(matrix(c(-2, 0, -1, 0, 0, 0), ncol = 2, byrow = TRUE)),
st_linestring(matrix(c(2, 0, 1, 0, 0, 0), ncol = 2, byrow = TRUE))
)
my_sf <- st_sf(
data.frame(my_text = c("1", "2", "3", "4")),
geometry = my_sfc,
crs = "EPSG:4326"
)
tm_shape(my_sf) +
tm_lines() +
tm_text("my_text", size = 2, along.lines = TRUE)

tm_shape(my_sf) +
tm_lines() +
tm_text(
"my_text",
size = 2,
along.lines = TRUE,
xmod = c(1, 1, 0, 0),
ymod = c(0, 0, 1, 1)
)

Created on 2023-02-18 with reprex v2.0.2