Search code examples
rtmap

tmap -- How can I format only part of a title text, while using a variable as part of the title?


Using the tmap package, is it possible to format only part of the text that makes up a title/axis label/legend entry etc, specifically when part of the name comes from a variable? In my case, I want the scientific name of a species to be italicised in a legend title, e.g. "Distribution of Falco perigrinus"

expression(paste(italic())) works to produce the format I'd like (i.e. "Continents with Falco peregrinus") if all the text is specified:

library(tmap)
data("World")

tm_shape(World) +
  tm_polygons(col = "continent",
              title = expression(paste("Continents with", italic("Falco peregrinus")))) +
  tm_layout(legend.outside = TRUE)

However, if the text I'd like to have italicised is stored as a variable, then it will return the name of the variable, rather than the value. i.e. "Continents with species":

species <- "Falco peregrinus"
tm_shape(World) +
  tm_polygons(col = "continent",
              title = expression(paste("Continents with", italic(species))))

Solution

  • Answer provided as a comment by Iroha works perfectly:

    title = parse(text = deparse(bquote("Continents with" ~ italic(.(species)))))