7

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")
sjarvie
  • 71
  • 1
  • 3
  • As far as I know the `fontface` argument is global = it covers all text in the map. – Jindra Lacko Nov 09 '18 at 21:06
  • thanks Jinda Lacko, that is also my understanding that the `fontface` argument is global from reading the help documentation. Do you know if it is possible to only change the font to _italics_ for the `main.title` argument in tmap? I also tried using `expression` terms in tmap to change the font style (for either the `main.title` or `legend`), but had no luck. Thanks in advance for any advice to make this change – sjarvie Nov 10 '18 at 12:45
  • This is an interesting problem - and as far as I know the only solutions are workarounds = plotting a static map via `geom_sf()` in ggplot, which has pretty advanced formatting, or dynamic map via leaflet which allows HTML tags – Jindra Lacko Nov 10 '18 at 21:01
  • thanks for the reply. I have used other _R_ packages for maps including **raster**, **rasterVis** and **ggplot2**, and now thought I would try **tmap** due to its many nice features, e.g. ease of formatting and not having to convert to a dataframe. The reason for the _italics_ in the `main.title` is to label a species' scientific name, while 'plain' text seems more appropriate for legends. One hacky way I found to change the legend title to plain was to put 'expression' around a new title, e.g. expression("World - HPI"). Not sure how to change the remainder of the legend text to plain though – sjarvie Nov 11 '18 at 17:42
  • I feel your pain; the italics for latin names are non negotiable in natural science. I am lucky not to need them, but in your use case you seem to be out of luck with `tmap` - you may consider filing an issue https://github.com/mtennekes/tmap/issues - to have the option included in future releases – Jindra Lacko Nov 12 '18 at 07:14
  • good suggestion re. filling in an issue. I am glad that it appears I have not missed anything obvious before making one. Best – sjarvie Nov 12 '18 at 08:29
  • 2
    following up on this, I created the issue on the github site. The author of tmap, Martijn, subsequently added 10 arguments to tm_layout (and therefore also tmap options) to be alter the fontface. These options are: a local fontface and fontfamily for the (map) title, main title, panel.label, legend.title and legend.text. This now gives great control over the font – sjarvie Dec 12 '18 at 05:00
  • Good job @sjarvie! I am certain this will be helpful to many others in natural sciences. I will look into the arguments later today. – Jindra Lacko Dec 12 '18 at 06:29
  • thanks Jinda. Hope you have the changes useful too – sjarvie Dec 13 '18 at 09:00

1 Answers1

2

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

Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68
  • thanks Valentin. Martijn, the author of tmap, added 10 arguments to tm_layout (and therefore also tmap options) to be alter the fontface. They are: a local fontface and fontfamily for the (map) title, main title, panel.label, legend.title and legend.text. This now gives great control over the font – sjarvie Dec 12 '18 at 04:56
  • Hi @sjarvie! Nice work! I encourage you to answer your own question with the updates that you mentioned. Take your initial code and add the new `tm_layout` arguments that solved your problem. – Valentin_Ștefan Dec 12 '18 at 09:15
  • Hi Valentin, good suggestion! I have edited my original post to add this information. Thanks – sjarvie Dec 13 '18 at 08:59