tmap R - change font for title of thematic plot from plain to italics but keep font of the legend plain

Viewed 2164

I would like to change the font for the main title of a thematic plot using the tmap package in R from 'plain' to italics but keep the font for the legend heading and text 'plain'.

However, when I change the argument fontface in the lm_layout() function it changes the font face of all text in the map. Is it possible to only change the font face for the main title in tmap?

My attempt at a reproducible example (which, unfortunately, changes the font face of all text in the map to italics) is below:

library(tmap)
data("World")

tm_shape(World) +
tm_polygons("HPI", title = "World - HPI") +
  tm_layout(main.title = "HPI",
            main.title.position = "center",
            fontface = 3) 

Edit: Martijn Tennekes, the author of the tmap package, has added 10 arguments to tm_layout (and therefore also tmap options) to allow control over this: a local fontface and fontfamily for the (map) title, main title, panel.label, legend.title and legend.text.

tm <- tm_shape(World) +
tm_polygons(c("HPI", "economy"), title = c("Legend 1", "Legend 2")) +
tm_layout(main.title = "Main Title",
          main.title.position = "center",
          title = c("Title 1", "Title 2"),
          panel.labels = c("Panel 1", "Panel 2"))

# global setting
tm + tm_layout(fontface = 3) 

# local setting
tm + tm_layout(main.title.fontface = 1, title.fontface = 2, panel.label.fontface = 3, legend.text.fontface = 4, legend.title.fontfamily = "serif")
1 Answers

Tested for tmap_2.1-1 version.

While the legend title in tm_polygons could be set to italic via title = expression(italic(your-text)), the plot title doesn't seem to allow expressions. One workaround would be to use the editing power of the grid package:

library(tmap)
library(grid)

data("World")

tm_shape(World) +
  tm_polygons("HPI", title = expression(italic(World - HPI))) + # set legend title to italic
  tm_layout(main.title = "HPI", # unfortunately, does not allow `expression`; try the `grid` hack
            main.title.position = "center") 

# Convert to gTree/list of grobs
g <- grid.grab()
View(g) # check the structure of the gTree; helps with identifying graphical elements
# Edit the fontface of the main title - from 1 (plain text) to 3 (italic); must be integer
g[["children"]][[1]][["children"]][["main_title"]][["children"]][[1]][["gp"]][["font"]] <- 3L
# Draw the edited gTree
grid.newpage(); grid.draw(g)

enter image description here

Related