tmap
tmap copied to clipboard
Title disaligned when set to be centered and bold
When setting title.fontface = 2 the title text gets longer than its own box, which end up making it not aligned. This is particularly visible when the title is long. See example below:
library(sf)
#> Warning: package 'sf' was built under R version 3.6.3
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(tmap)
library(spData)
world_map <- tm_shape(spData::world) + tm_polygons() +
tm_layout(
title = "Big Title World Map",
title.position = c("center", "top"),
title.bg.color = "gray50",
title.size = 1.5,
title.fontface = 1,
inner.margins = c(0.02, 0.03, 0.2, 0.02)
)
print(world_map)

world_map <- tm_shape(spData::world) + tm_polygons() +
tm_layout(
title = "Big Title World Map",
title.position = c("center", "top"),
title.bg.color = "gray50",
title.size = 1.5,
title.fontface = 2,
inner.margins = c(0.02, 0.03, 0.2, 0.02)
)
print(world_map)

Created on 2020-05-11 by the reprex package (v0.3.0)
I think the problem is that grid::stringWidth doesn't take font face, or even font type into account.
I recall that someone suggested me a package that does this, but I can't remember it's name...
It works in tmap v4.
library(sf)
library(tmap)
library(spData)
world_map = tm_shape(spData::world) + tm_polygons() +
tm_title_in("Big Title World Map",
position = c("center", "top"),
frame = TRUE,
bg.color = "gray50") +
tm_layout(inner.margins = c(0.02, 0.03, 0.2, 0.02))
print(world_map)
