在 R 中使用 tmap 时如何将指南针箭头和比例尺移出绘图?
我正在使用 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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还可以使用内部边距将图例和属性放置在外部。
(这在 tmap4 中会有所不同(更容易))
You can also use inner margins to place legends and attributes outside.
(This will be different (easier) in tmap4)