如何在拥挤的 tmap 图例 [R] 中修复垂直 space

How to fix vertical space in crowded tmap legend [R]

如何修复 tmap 图例中的垂直 space 问题,例如链接的 base R 示例中显示的问题? (y.intersp 不是 tmap_add_legend() 识别的参数)

基本tmap代码:

library(sf)
library(tmap)
tm_shape(st_read(system.file('shape/nc.shp', package = 'sf'))) + 
  tm_polygons() + 
  tm_add_legend(
    type = 'symbol', 
    labels = c('Variable A', 'Variable B', 'Variable C', 'Variable D', 'Variable E'), 
    col = c('#832424FF', 'rosybrown4', 'red', 'red', '#4F8DC2'),
    shape = c(19, 19, 4, 5, 15)
  ) 

我无法完全按照您的要求找到图例项填充,但如果它符合您的目的,您可以稍微调整 sizelegend.text.size

如果您想在单词之间添加填充但又不介意图例图标看起来 bigger/still 靠得很近,

s <- st_read(system.file('shape/nc.shp', package = 'sf'))
tm_shape(s) + 
tm_polygons() + 
tm_add_legend(
    type = 'symbol', 
    labels = c('Variable A', 'Variable B', 'Variable C', 'Variable D', 'Variable E'), 
    col = c('#832424FF', 'rosybrown4', 'red', 'red', '#4F8DC2'),
    shape = c(19, 19, 4, 5, 15), 
    size = 1.2
) + 
tm_layout(legend.text.size = 0.8)

如果您想在图例图标之间放置内边距,但文本标签之间的距离可以,

tm_shape(s) + 
tm_polygons() + 
tm_add_legend(
    type = 'symbol', 
    labels = c('Variable A', 'Variable B', 'Variable C', 'Variable D', 'Variable E'), 
    col = c('#832424FF', 'rosybrown4', 'red', 'red', '#4F8DC2'),
    shape = c(19, 19, 4, 5, 15), 
    size = 0.5
) + 
tm_layout(legend.text.size = 1.0)

我希望有人找到一个可以调整两个填充的更好的选项,但这同时可能会满足您的目的。