-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2015-map.rmd
225 lines (179 loc) · 4.59 KB
/
2015-map.rmd
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# R语言采样地图绘制详细教程
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
## library(tidyverse) # Wickham的数据整理的整套工具
pdf.options(height = 10 / 2.54, width = 10 / 2.54, family = "GB1") # 注意:此设置要放在最后
```
> 本期主要介绍如何绘制采样地图和国内区域地图,并自定义相关内容
## R包介绍
[ggspatial教程](https://cran.r-project.org/web/packages/ggspatial/index.html)
## R包安装
```{r eval=FALSE}
install.packages("ggspatial")
install.packages("sf")
```
## 基础绘图代码
### 导入地图json数据
进入从[阿里云DataV可视化网站](http://datav.aliyun.com/portal/school/atlas/area_selector)(可选择其他平台)下载格式为.json的地图数据:
![](https://pic-go-42.oss-cn-guangzhou.aliyuncs.com/img/202410270835984.webp)
### 数据导入
```{r}
library(ggspatial)
library(sf)
library(ggplot2)
# 导入地图json数据
map <- st_read("01-attch\\10\\广州市.json")
```
### 开始绘制
#### 添加省份区边框
```{r collapse=TRUE}
ggplot() +
labs(title = "Guangzhou", x = NULL, y = NULL) +
geom_sf(data = map, fill = c("#f0eedf"), size = 0.8, color = "black")
```
#### 添加指南针annotation
```{r}
p <- ggplot() +
labs(title = "Guangzhou", x = NULL, y = NULL) +
geom_sf(data = map, fill = c("#f0eedf"), size = 0.8, color = "black") + # 设置比例尺
annotation_north_arrow(
location = "tl",
style = north_arrow_nautical(
fill = c("black", "white"),
line_col = "black"
)
)
p
```
## 我们来自定义绘图内容
### 设置白云区天河区突出显示
我们新加一个图层就可以,然后fill填充亮色
```{r}
p + geom_sf(
data = map |> dplyr::filter(name %in% c("天河区", "白云区", "番禺区")),
fill = c("#c98c50"),
size = 0.8,
color = "black"
)
```
### 设置text
接下来我们要在图里标注部分区名
```{r message = F}
library(showtext)
showtext::showtext_auto()
```
```{r warning = F}
p + geom_sf(
data = map |> dplyr::filter(name %in% c("天河区", "白云区", "番禺区")),
fill = c("#c98c50"),
size = 0.8,
color = "black"
) +
geom_sf_text(
data = map |> dplyr::filter(name %in% c("天河区", "白云区", "番禺区")),
aes(label = name),
size = 3,
color = "black",
fontface = "bold"
)
```
### 设置一个合适的主题
```{r warning = F}
p2 <-
p + geom_sf(
data = map |> dplyr::filter(name %in% c("天河区", "白云区", "番禺区")),
fill = c("#c98c50"),
size = 0.8,
color = "black"
) +
geom_sf_text(
data = map |> dplyr::filter(name %in% c("天河区", "白云区", "番禺区")),
aes(label = name),
size = 3,
color = "black",
fontface = "bold"
) +
theme_minimal()
p2
```
### 设置根据数值变量对各区的fill进行映射
```{r warning = F}
# 先生成一个随机变量
map_neat_1 <-
map |>
dplyr::mutate(
value = sample(1:100, nrow(map), replace = TRUE)
)
ggplot() +
labs(title = "map_neat_1", x = NULL, y = NULL) +
geom_sf(data = map_neat_1, aes(fill = value), size = 0.8, color = "black") + # 设置比例尺
annotation_north_arrow(
location = "tl",
style = north_arrow_nautical(
fill = c("black", "white"),
line_col = "black"
)
) +
geom_sf_text(
data = map,
aes(label = name),
size = 3,
color = "black",
fontface = "bold"
) +
theme_minimal()
```
### 根据分类变量对区进行fill映射
```{r warning = F}
library(MetBrewer)
map_neat_2 <-
map |>
dplyr::mutate(
group = sample(c("A", "B", "C"), nrow(map), replace = TRUE)
)
ggplot() +
labs(title = "map_neat_2", x = NULL, y = NULL) +
geom_sf(data = map_neat_2, aes(fill = group), size = 0.8, color = "black") + # 设置比例尺
annotation_north_arrow(
location = "tl",
style = north_arrow_nautical(
fill = c("black", "white"),
line_col = "black"
)
) +
geom_sf_text(
data = map,
aes(label = name),
size = 3,
color = "black",
fontface = "bold"
) +
theme_minimal() +
scale_fill_met_d("Cassatt1")
```
### 添加采样点
```{r warning = F}
data_sample <-
tibble::tibble(
lon = c(113.292333, 113.412333, 113.532333),
lat = c(23.191944, 23.331944, 22.931944),
point = c("A", "B", "C"),
)
p2 +
geom_point(
data = data_sample,
aes(x = lon, y = lat),
size = 2,
color = "#1647a3"
) +
geom_text(
data = data_sample,
aes(x = lon, y = lat, label = point),
size = 4,
color = "#000000",
fontface = "bold",
nudge_y = 0.05
)+
labs(title = "广州市")+
theme(plot.title = element_text(hjust = 0.5))
```