2

I have encountered some unexpected behavior in the tmap library and was wondering if there is a way around it. When producing a map with an expression in the legend title, the legend becomes too wide for some reason.

Here's some code to show what I mean:

library(tmap)
data("World")

# Produces normal legend window
tm_shape(World) +
  tm_polygons("HPI") +
  tm_layout(legend.bg.color = "#AAAAAA")

# Produces wide legend window
title <- expression(Local~G[i]^"*")
tm_shape(World) +
  tm_polygons("HPI", title = title) +
  tm_layout(legend.bg.color = "#AAAAAA")

I tried using the legend.width parameter of tm_layout, but that messes up the font and symbol size down the line. Any ideas how to fix this? Thanks!

karpfen
  • 489
  • 8
  • 19

1 Answers1

0

It seems like tmap just takes the unparsed length of your title as the legend.width and since your expression width is substantial due to sub/superscripts, you end up with a very wide frame. I couldn't find a way to prevent tmap from rescaling the text when legend.width is used. One solution would be to just remove the background color. Not ideal, but takes care of the weird width issue.

Arienrhod
  • 2,451
  • 1
  • 11
  • 19
  • That's not an option for me unfortunately. There's a lot going on on my base map, so I need a background to make it readable. – karpfen Sep 18 '19 at 12:03
  • Maybe you should consider `ggplot` then. Definitely more flexible when it comes to legend modifications. – Arienrhod Sep 18 '19 at 12:44