ihb*_*ihb 3 r spatial tmap r-sf
我正在使用该tmap包在地图上绘制一些数据。由于数据集的大小,您可以在此处下载所需的数据集(它们是公共数据集)。此代码使用驱动器中的.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"))
Run Code Online (Sandbox Code Playgroud)
生成结果图。但是,我需要移动指南针和比例尺,如下图所示:
您还可以使用内部边距将图例和属性放置在外部。
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)
Run Code Online (Sandbox Code Playgroud)
(这在 tmap4 中会有所不同(更容易))