Tmap: fontface not global

Created on 12 Nov 2018  ·  8Comments  ·  Source: mtennekes/tmap

Dear tmap-developers,

I would like to change the font for the main title from 'plain' to _italics_ but keep the font for the legend heading and text 'plain'. I tried the fontface argument, however, it appears to be global (as stated in the help documentation) and changes the font face of all text. I, therefore, wondered if it is possible to only change the font face for part of the map? The reason for not needing a global change is to use latin names that need to be in _italics_, while preferring the remainder of the map to have 'plain' text.

library(tmap)
data("World")

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

A hacky solution I found to not change the legend title was to wrap it in an expression term, e.g.

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

However, I could not find a way to change the legend text in a similar fashion.

Thanks for your help here.

question

All 8 comments

Done! Added 10 arguments to tm_layout (and therefore also tmap options): 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")

Hopefully, it is still possible to see the trees through the woods with so many arguments/options... Otherwise, we should follow a similar approach as ggplot2 with its element_text like functions. What do you think? (@Nowosad and @Robinlovelace)

It's hard to tell what's the best solution. If I understand it correctly, there are two options:

  1. Tons of simple arguments, such as title.fontface, title.fontfamily, title.fontsize, etc.
  2. Way less, but more complex arguments, such as title = element_text(fontface = XX, fontfamily = YY, fontsize = ZZ)

@mtennekes is there more to it than font face, family, and size?

For the static text, it's also color.

There is much more repetition in the aesthetic layers, e.g. in tm_symbols there are legend.size.show, legend.size.is.portrait, legend.size.reverse, not only for size but also for col and shape. It would take quite some effort to group the arguments and use functions like element_text.

So I'll leave this issue for now...

Hi @mtennekes,

thanks for looking into my question. To clarify, have the arguments you mentioned you added above in the first response been implemented in tmap? If so, I'm currently not able to get them to work, e.g. I have tried running the code, installing the development version of map and running the code. Or have you shelved this issue entirely for the time being.

Thanks for your help. I do think the ability to change the fontface will be useful, but do see why from what you are saying above you may not want to do it now.

Best

Hi Scott, the code above should work with the latest version. If not, let me know what error messages you get.

Hi Martijn,

thanks for the prompt reply.

I have tried to install the latest version of tmap from both cran (using install.packages) and the development version from GitHub (using devtools as well as paceman::p_load_gh) on a macOS with R version 3.5.1 (2018-07-02) -- "Feather Spray".

When I run the line of code:

# global setting tm + tm_layout(fontface = 3)

There are no errors.

When I run the line of code

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

The error message is: Error in tm_layout(main.title.fontface = 1, title.fontface = 2, panel.label.fontface = 3, :
unused arguments (main.title.fontface = 1, title.fontface = 2, panel.label.fontface = 3, legend.text.fontface = 4)

I also get an error message when just using one argument, for instance:

tm + tm_layout(main.title.fontface = 2)

The error message is: Error in tm_layout(main.title.fontface = 2) :
unused argument (main.title.fontface = 2)

Thanks for helping me find out where I am going wrong! If you need anymore information, do say!

Have you reinstalled tmap?

library(devtools)
install_github("mtennekes/tmaptools")
install_github("mtennekes/tmap")

What is your session info?

sessionInfo()

Hi @Nowosad and @mtennekes,

I had thought I had reintalled tmap (was getting this message when I ran it this morning: Skipping install of 'tmap' from a github remote, the SHA1 (1fdf5c21) has not changed since last install. Use force = TRUE to force installation), but the code was not working. Now, I restarted the computer and reinstalled with devtools after the latest message, and it works perfectly! I look forward to using these _new_ arguments in my work! Thanks again for your help!

My session info for the successful run (not sure if it is still needed) is:
─ Session info ──────────────────────────────────────────────────────────────────────────────────
setting value
version R version 3.5.1 (2018-07-02)
os macOS High Sierra 10.13.6
system x86_64, darwin15.6.0
ui RStudio
language (EN)
collate en_NZ.UTF-8
ctype en_NZ.UTF-8
tz Europe/Copenhagen
date 2018-12-01

─ Packages ──────────────────────────────────────────────────────────────────────────────────────
package * version date lib source
assertthat 0.2.0 2017-04-11 [1] CRAN (R 3.5.0)
backports 1.1.2 2017-12-13 [1] CRAN (R 3.5.0)
base64enc 0.1-3 2015-07-28 [1] CRAN (R 3.5.0)
callr 3.0.0 2018-08-24 [1] CRAN (R 3.5.0)
class 7.3-14 2015-08-30 [1] CRAN (R 3.5.1)
classInt 0.2-3 2018-04-16 [1] CRAN (R 3.5.0)
cli 1.0.1 2018-09-25 [1] CRAN (R 3.5.0)
codetools 0.2-15 2016-10-05 [1] CRAN (R 3.5.1)
colorspace 1.3-2 2016-12-14 [1] CRAN (R 3.5.0)
crayon 1.3.4 2017-09-16 [1] CRAN (R 3.5.0)
crosstalk 1.0.0 2016-12-21 [1] CRAN (R 3.5.0)
curl 3.2 2018-03-28 [1] CRAN (R 3.5.0)
DBI 1.0.0 2018-05-02 [1] CRAN (R 3.5.0)
desc 1.2.0 2018-05-01 [1] CRAN (R 3.5.0)
devtools * 2.0.1 2018-10-26 [1] CRAN (R 3.5.1)
dichromat 2.0-0 2013-01-24 [1] CRAN (R 3.5.0)
digest 0.6.18 2018-10-10 [1] CRAN (R 3.5.0)
e1071 1.7-0 2018-07-28 [1] CRAN (R 3.5.0)
fs 1.2.6 2018-08-23 [1] CRAN (R 3.5.0)
glue 1.3.0 2018-07-17 [1] CRAN (R 3.5.0)
htmltools 0.3.6 2017-04-28 [1] CRAN (R 3.5.0)
htmlwidgets 1.3 2018-09-30 [1] CRAN (R 3.5.0)
httpuv 1.4.5 2018-07-19 [1] CRAN (R 3.5.0)
KernSmooth 2.23-15 2015-06-29 [1] CRAN (R 3.5.1)
later 0.7.5 2018-09-18 [1] CRAN (R 3.5.0)
lattice 0.20-35 2017-03-25 [1] CRAN (R 3.5.1)
leaflet 2.0.2 2018-08-27 [1] CRAN (R 3.5.0)
lwgeom 0.1-4 2018-01-28 [1] CRAN (R 3.5.0)
magrittr 1.5 2014-11-22 [1] CRAN (R 3.5.0)
mapview 2.6.0 2018-09-16 [1] CRAN (R 3.5.0)
memoise 1.1.0 2017-04-21 [1] CRAN (R 3.5.0)
mime 0.6 2018-10-05 [1] CRAN (R 3.5.0)
munsell 0.5.0 2018-06-12 [1] CRAN (R 3.5.0)
pkgbuild 1.0.2 2018-10-16 [1] CRAN (R 3.5.0)
pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.5.0)
png 0.1-7 2013-12-03 [1] CRAN (R 3.5.0)
prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.5.0)
processx 3.2.0 2018-08-16 [1] CRAN (R 3.5.0)
promises 1.0.1 2018-04-13 [1] CRAN (R 3.5.0)
ps 1.2.1 2018-11-06 [1] CRAN (R 3.5.0)
R6 2.3.0 2018-10-04 [1] CRAN (R 3.5.0)
raster 2.8-4 2018-11-03 [1] CRAN (R 3.5.0)
RColorBrewer 1.1-2 2014-12-07 [1] CRAN (R 3.5.0)
Rcpp 1.0.0 2018-11-07 [1] CRAN (R 3.5.0)
remotes 2.0.2 2018-10-30 [1] CRAN (R 3.5.0)
rgdal 1.3-6 2018-10-16 [1] CRAN (R 3.5.0)
rgeos 0.4-2 2018-11-08 [1] CRAN (R 3.5.0)
rlang 0.3.0.1 2018-10-25 [1] CRAN (R 3.5.0)
rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.5.0)
rstudioapi 0.8 2018-10-02 [1] CRAN (R 3.5.0)
satellite 1.0.1 2017-10-18 [1] CRAN (R 3.5.0)
scales 1.0.0 2018-08-09 [1] CRAN (R 3.5.0)
sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.5.0)
sf 0.7-1 2018-10-24 [1] CRAN (R 3.5.0)
shiny 1.2.0 2018-11-02 [1] CRAN (R 3.5.0)
sp 1.3-1 2018-06-05 [1] CRAN (R 3.5.0)
spData 0.2.9.4 2018-09-15 [1] CRAN (R 3.5.0)
testthat 2.0.1 2018-10-13 [1] CRAN (R 3.5.0)
tmap * 2.2 2018-12-01 [1] Github (mtennekes/tmap@1fdf5c2)
tmaptools * 2.0-1 2018-12-01 [1] Github (mtennekes/tmaptools@feb8a3f)
units 0.6-1 2018-09-21 [1] CRAN (R 3.5.0)
usethis * 1.4.0 2018-08-14 [1] CRAN (R 3.5.0)
viridisLite 0.3.0 2018-02-01 [1] CRAN (R 3.5.0)
webshot 0.5.1 2018-09-28 [1] CRAN (R 3.5.0)
withr 2.1.2 2018-03-15 [1] CRAN (R 3.5.0)
XML 3.98-1.16 2018-08-19 [1] CRAN (R 3.5.0)
xtable 1.8-3 2018-08-29 [1] CRAN (R 3.5.0)
yaml 2.2.0 2018-07-25 [1] CRAN (R 3.5.0)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

carbonmetrics picture carbonmetrics  ·  3Comments

dylanbeaudette picture dylanbeaudette  ·  3Comments

Nowosad picture Nowosad  ·  4Comments

Si-Chong picture Si-Chong  ·  4Comments

marionowak picture marionowak  ·  9Comments