0

I am trying to create a map with the tmap showing the population density. Therefore, I want the legend title of the map to be "Population density in n\ [people per km^2]". This map is the closest I have gotten to my desired final result.

Now, as you see, I manage to insert a line break but somehow the last "]" jumps all to the right and the title is cut half off in the first line. I think the latter is a problem I can solve by myself, but I don't find a solution to get rid off the former problem.

To create this map, I used the following code

tm_polygons("Pop_dens",title=expression(paste("Population density \n[people per km"^"2","]")),breaks=c(0,50,100,500,1000,3000,5000,25000),border.alpha = 0)+
  tm_shape(border_plateau)+
  tm_polygons(alpha=0)+
  tm_scale_bar(breaks=c(0,10,20,50,100),position = c(0.56,0.04))+
  tm_compass(position = c(0.4,0.04))+
  tm_legend(legend.position=c(0.71,0.11))

How can I remove the space (ergo make it left-binding)? I am familiar with making new lines in the legend using \n and pasting expressions using expression(paste(..)), but combining both doesn't seem to work.

2 Answers2

0

One option would be to use:

title=expression(atop("Population density","(people per km"^2*")"))

Although the space between lines may be excessively large

davidnortes
  • 872
  • 6
  • 14
0

I faced the same problem and solved it by using HTML tags with the package ‘htmltools’:

title=paste('Line 1', br('Line 2'))
Daniela
  • 701
  • 1
  • 3
  • 4