简体   繁体   English

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

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

How do I fix vertical space issues in tmap legends, like the ones shown in the linked base R example?如何修复 tmap 图例中的垂直空间问题,例如链接的基本 R 示例中显示的问题? ( Vertical spaces in legend ; y.intersp is not a recognized parameter by tmap_add_legend() ) 图例中的垂直空间y.intersp不是tmap_add_legend()识别的参数)

Basic tmap code:基本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)
  ) 

I couldn't quite find the legend-item padding as you requested, but if it serves your purpose, you can adjust size and legend.text.size a bit.我无法按照您的要求找到图例项填充,但如果它符合您的目的,您可以legend.text.size调整sizelegend.text.size

If you want to put padding between the words but don't mind the legend icons looking bigger/still close together,如果您想在单词之间添加填充但不介意图例图标看起来更大/仍然靠得很近,

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)

If you want to put padding between legend icons but distance between text labels are okay,如果您想在图例图标之间放置填充但文本标签之间的距离没问题,

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)

I hope somebody finds a better option that can adjust both paddings, but this may serve your purpose in the meantime.我希望有人能找到一个更好的选择来调整两个内边距,但这同时也可以满足您的目的。

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

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