在 R 中使用 tmap 时如何将罗盘箭头和比例尺移出绘图?

How to move the compass arrow and scale bar off of the plot when using tmap in R?

我正在使用 tmap 包在地图上绘制一些数据。由于数据集的大小,驱动器中有 you can download the needed ones here (they are public datasets). This code uses the .csv and .shp 个文件。

我需要将指南针和比例尺从图像上移到图例下方。我怎样才能做到这一点?我还没有看到 tm_compass 命令有一个选项可以将它移到图像之外,那么还有另一种方法吗?

代码:

#Load packages
library(pacman)
p_load(sf, tidyverse, ggspatial, tmap, ggplot2)

#Read data
boundary <- st_read('US_tract_2010.shp')
food <- read.csv('FoodAtlas.csv')

#Join data together
boundary$GEOID10 <- as.numeric(boundary$GEOID10)
foodleft <- left_join(boundary, food, by = c('GEOID10' = 'CensusTract'))


#Construct the graph
ks <- foodleft %>%
  select(State, County, TractLOWI, LAhalfand10) %>% 
  filter(State %in% c('Kansas'))

ks$LAhalfand10 <- as.factor(ks$LAhalfand10)

tmap_mode("plot")

qtm(ks, 
    fill = "LAhalfand10",
    fill.title = "LAhalfand10") +
  tm_bubbles("TractLOWI", col = "steelblue", title.size = "TractLOWI (Pop)") +
  tm_layout(legend.outside = TRUE, frame = FALSE) +
  tm_compass(size = 3, type = 'rose', position = c("right","top")) +
  tm_scale_bar(size = 0.5, position = c("RIGHT", "BOTTOM"))

结果图生成。但是,我需要移动指南针和比例尺,如下图所示:

您还可以使用内边距将图例和属性放在外面。

data(World)
qtm(World, 
    fill = "economy") +
    tm_layout(inner.margins = c(0.02, 0.02, 0.02, 0.3)) +
    tm_compass(size = 3, type = 'rose') +
    tm_scale_bar(size = 0.5)

(这在 tmap4 中会有所不同(更容易))