简体   繁体   English

在 tmap 图例中使用上标时格式化换行符

[英]Formatting line breaks when using superscripts in tmap legend

I am trying to create a map with the tmap showing the population density.我正在尝试使用显示人口密度的 tmap 创建一个 map。 Therefore, I want the legend title of the map to be "Population density in n\ [people per km^2]".因此,我希望 map 的图例标题为“n 中的人口密度 [每公里 ^ 2 人]”。 This map is the closest I have gotten to my desired final result.这个map是我最接近我想要的最终结果的。

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为了创建这个 map,我使用了以下代码

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)?如何删除空间(ergo 使其左绑定)? 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.我熟悉使用 \n 在图例中创建新行并使用表达式(粘贴(..))粘贴表达式,但将两者结合起来似乎不起作用。

One option would be to use:一种选择是使用:

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

Although the space between lines may be excessively large虽然行间距可能过大

I faced the same problem and solved it by using HTML tags with the package 'htmltools':我遇到了同样的问题,并通过使用带有 package 'htmltools' 的 HTML 标签来解决它:

title=paste('Line 1', br('Line 2'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM