用tmap程序包绘制带指北针、图例和经纬网的采样点地图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
setwd("C:/Users/jlzhang/Desktop/sample_map")
#### 导入所需要的程序包
library(rgdal)
library(tmap)
library(tmaptools)
library(sp)
rm(list = ls())
## 读取地图
country <- readOGR("bou1_4l.shp")
province <- readOGR("province_polygon.shp")
world <- readOGR("ne_50m_admin_0_countries.shp")
# Source: https://www.naturalearthdata.com/downloads/50m-cultural-vectors/
world <- set_projection(world, "longlat")
country <- set_projection(country, "longlat")
province <- set_projection(province, "longlat")
province <- set_projection(province, "longlat")
data(World)
# 城市
city <- read.csv("simplemaps-worldcities-basic.csv", header = TRUE)
city <- city[sample(1:nrow(city), 400),] # 随机筛选400个
coordinates(city) <- ~lng+lat
proj4string(city) <- CRS("+proj=longlat +datum=WGS84")

World$name[World$name == "Taiwan"] <- NA
# 转换为spatial dataframe
# 绘图
tm_shape(world,
xlim = c(60, 140),
ylim = c(0, 60)) +
tm_borders("grey40", lwd = 1.5) +
tm_grid()+
tm_shape(province) +
tm_fill(col = "lightgrey") +
tm_borders("grey60", lwd = 0.8) +
tm_shape(country) +
tm_lines(col="grey40", lwd = 1.5) +
tm_scale_bar(position=c(0.05,0.0)) +
tm_compass(type = "4star",
position=c("left", "top")) +
tm_layout(inner.margins=c(0.12,0.03,0.08,0.03),
legend.stack = "horizontal") +
tm_shape(city) +
tm_bubbles("pop",
col = "red",
scale=.5,
border.col = "red") +
tm_text("city", size=0.5) +
tm_legend(legend.position = c(0.05, 0.08)) +
tm_shape(World) +
tm_text("name", size=1.0)