4

First time posting on stackoverflow. I've been learning to work with tmap, and have gotten almost everything down for the choropleth maps that I need. However, the finishing touch that I can't seem to find an answer to is making the legend title bigger than the text below it. Would like to make it bold as well. Here is an example of my code:

tm_shape(Shakira) + 
tm_polygons("mydata3$nkill", textNA="Missing Data", 
title="Deaths From Terrorism", 
          contrast = 1, style = "kmeans",
          palette = "Greens") +
tm_text("NAME_1", size = "AREA") +
tm_style_natural() +
tm_legend(position = c("right", "top"),
        bg.color = "white",
        bg.alpha=.2,
        width = .25, title.size = 5) +
tm_layout(legend.title.size = .5, legend.text.size = .65,
        legend.frame = TRUE)

I thought it would be legend.title.size, however all that does is increase or decrease the space between the title and the text below it, not make the font any different. I appreciate any help with this!

www
  • 38,575
  • 12
  • 48
  • 84
Cody Brown
  • 41
  • 1
  • 2

2 Answers2

2

To set the font size you indeed need the title.size, just be aware of the difference between legend.title.size for legend title and plain title.size for the main chart title.

To make the text bold use fontface argument, it follows the R standard - bold is 2, italics 3.

Your example is not exactly reproducible, so I came up with a quick one of mine:

library(tmap)
data(Europe)

Europe <- Europe[!is.na(Europe$EU_Schengen), ] # to make it cleaner

tm_shape(Europe) + tm_borders() +
  tm_style_white("A map with normal title", frame = F, title.size = 1)


tm_shape(Europe) + tm_borders() +
  tm_style_white("A map with big title", frame = F, title.size = 2) 

tm_shape(Europe) + tm_borders() +
  tm_style_white("A map with bold title", frame = F, fontface = 2)

This is the normal title: enter image description here This is the big one: enter image description here And this is the bold one: enter image description here

Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44
  • This answer does not address the setting of the legend title font size nor the fact that `legend.title.size` seems to only control the space between the legend title and the text below it, but not the legend title font size. – syre Jun 18 '20 at 09:25
0

I was able to change the font of the legend title by adjusting the legend.title.size and legend.width at the same time.