Label a map with lead lines/call out using tmap in R
Asked Answered
U

0

6

I am trying to produce a map using the tmap library. I want to label all areas and use lead lines to label tiny areas. I'm looking for a line of code that works like ggrepel in ggplot2 or geom_sf_label_repel in the sf library but for tmap.

Here is a sample code which shows all labels but no lead lines outside the polygons:

library(tmap)
data(World)
tm_shape(World) + tm_polygons("HPI") + tm_text("name",size=0.4)

THis is the result:

Image showing labels on the map using tm_text with tmap

There are overlapping labels that could have been placed in the white spaces with leading lines to the polygons

This is the kind of end result I hope to have where the polygon is too small to show a label correctly:

Labels outside plot area

How do I achieve this with tmap with an actual shapefile?

Uther answered 10/7, 2023 at 23:15 Comment(1)
I have not seen this functionality in tmap. There is an option to manually modify the x and y coordinates of each label with respect to actual location (by passing a vector to xmod and ymod in tm_text), but I haven't found a way to link those text labels back to their actual location using a line. Maybe that could be a feature suggestion you could make on the package's GitHub?Zoarah

© 2022 - 2024 — McMap. All rights reserved.