In "R" during plotting "tmap" library places labels outside plotting region (partially)

In "R" during plotting "tmap" library places labels outside plotting region (partially)

我正在将 shapefile 导入 R 并尝试用标签绘制它。不幸的是,有些标签重叠。这就是为什么我必须为“tm_text”函数使用参数“auto.placement = T”。但是这个参数将一些标签放置在绘图区域之外(部分)。每个图上标签的位置是随机的。有时标签在绘图区域,但大多数时候没有(切割)。

如您在屏幕截图中所见,"Palangos m." 被缩减为 "angos m.","Klaipedos m." 被缩减为 "aipedos m."。

截图:map

tm_shape(area_r1) + 
  tm_fill("winner", title = "Winner", style = "cat", 
      palette = c("#FFFFB3", "#1F78B4", "#1A9850", "#E7298A") ) +
  tm_legend(text.size = 0.75) +
  tm_layout("", legend.position = c("left", "bottom")) +
  tm_borders("grey60") +
  tm_layout(frame = F) +
  tm_text("savivald", size = .65, col = "black", auto.placement = T)

我该怎么做才能使这个标签适合绘图区域?

控制 auto.placement = T 的随机性很困难(尽管设置种子可能有所帮助)。

可以做的是调整你的tmap物体的边界框哦,稍微调整一下,这样左边就有更多的空间给两个左右丢失的字母。

将bbox增加一半可能有点夸张,但您可以根据需要进行调整。

bbox_new <- st_bbox(area_r1) # current bounding box

xrange <- bbox_new$xmax - bbox_new$xmin # range of x values
yrange <- bbox_new$ymax - bbox_new$ymin # range of y values

  bbox_new[1] <- bbox_new[1] - (0.5 * xrange) # xmin - left
# bbox_new[3] <- bbox_new[3] + (0.5 * xrange) # xmax - right
# bbox_new[2] <- bbox_new[2] - (0.5 * yrange) # ymin - bottom
# bbox_new[4] <- bbox_new[4] + (0.5 * yrange) # ymax - top

bbox_new <- bbox_new %>%  # take the bounding box ...
  st_as_sfc() # ... and make it a sf polygon

tm_shape(area_r1, bbox = bbox_new) + 
  tm_fill("winner", title = "Winner", style = "cat", 
      palette = c("#FFFFB3", "#1F78B4", "#1A9850", "#E7298A") ) +
  tm_legend(text.size = 0.75) +
  tm_layout("", legend.position = c("left", "bottom")) +
  tm_borders("grey60") +
  tm_layout(frame = F) +
  tm_text("savivald", size = .65, col = "black", auto.placement = T)

前段时间我写了一篇博客post总结技术。 https://www.jla-data.net/eng/adjusting-bounding-box-of-a-tmap-map/

您的示例不能完全重现,但这张图片(为北卡罗来纳州地图上的 "big fat, title" 制作更多 space)应该可以让您了解。

@Jinda Lacko 使用 bbox 有一个创造性的解决方案,但由于某种原因它对我不起作用。一个简单的解决方案是在 tm_layout 中使用 main.title,例如 tm_layout(main.title = "Big Fat Title")。有关详细信息,请参阅此