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

1

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")
Twitt answered 7/11, 2018 at 16:38 Comment(9)
As far as I know the fontface argument is global = it covers all text in the map.Notation
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 changeTwitt
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 tagsNotation
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 thoughTwitt
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 github.com/mtennekes/tmap/issues - to have the option included in future releasesNotation
good suggestion re. filling in an issue. I am glad that it appears I have not missed anything obvious before making one. BestTwitt
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 fontTwitt
Good job @sjarvie! I am certain this will be helpful to many others in natural sciences. I will look into the arguments later today.Notation
thanks Jinda. Hope you have the changes useful tooTwitt
S
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

Scabies answered 11/12, 2018 at 0:2 Comment(3)
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 fontTwitt
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.Myceto
Hi Valentin, good suggestion! I have edited my original post to add this information. ThanksTwitt

© 2022 - 2024 — McMap. All rights reserved.