tmap icon indicating copy to clipboard operation
tmap copied to clipboard

Title disaligned when set to be centered and bold

Open dhersz opened this issue 5 years ago • 1 comments

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)

dhersz avatar May 11 '20 14:05 dhersz

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...

mtennekes avatar May 28 '20 06:05 mtennekes

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)

Nowosad avatar Sep 18 '23 07:09 Nowosad