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

发布于 2025-01-11 07:18:54 字数 1629 浏览 3 评论 0原文

我正在使用 tmap 包在地图上绘制一些数据。由于数据集的大小,您可以在此处下载所需的数据< /a> (它们是公共数据集)。此代码使用 .csv.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"))

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

在此处输入图像描述

I am using the tmap package to plot some data on a map. Because of the size of the datasets, you can download the needed ones here (they are public datasets). This code uses the .csv and .shp files in the drive.

I need to move the compass and the scale bar off of the image and below my legends. How can I do this? I haven't seen that the tm_compass command has an option to move it outside of the image, so is there another way of doing this?

Code:

#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"))

The resulting graph is produced. However, I need to move the compass and the scale bar as indicated in the image below:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2025-01-18 07:18:54

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

example

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 中会有所不同(更容易))

You can also use inner margins to place legends and attributes outside.

example

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)

(This will be different (easier) in tmap4)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文