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
|
# 加载做数据所需包
library(sp)
library(maptools)
library(tidyverse)
# 读取地图数据
china_map=readShapePoly("~/map/data/bou2_4p.shp")
# 生成做地图所需数据
china_map %>%
fortify() %>% # 地图数据转化为数据框,获得long、lat和id
mutate(id = as.numeric(id)) %>%
left_join(data.frame(china_map@data, id=seq(0:924)-1), by="id")->china_mapdata
# 读取人口信息NAME、pop、city
pop=read.csv("~/map/data/pop.csv",stringsAsFactors = FALSE)
# 读取省会城市经纬度数据city、jd、wd
position=read.csv("~/map/data/citylonlat.csv",stringsAsFactors = FALSE)
# 生成地图上作图所需信息
china_pop=left_join(pop, position, by="city")
# 利用ggplot2生成地图
ggplot()+
geom_polygon(data = china_mapdata, alpha = 0.8,
aes(x = long,y = lat,group = group,fill = NAME))+
geom_path(colour = "grey40")+
scale_fill_manual(values=colours()[2:34],guide=FALSE)+
geom_errorbar(data=china_pop,aes(x=jd, ymin=wd, ymax=wd+pop/3000000),
color="red",size=5, width=0,alpha=0.5)+
geom_text(data =china_pop,aes(x=jd,y=wd,label=city),colour="black",size=3,
vjust=0,nudge_y=0.5)+
ggtitle("\n中华人民共和国人口分布图")+
theme(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.background = element_blank(),
plot.title = element_text(hjust = 0.5,
size = 20,
face = "bold",
color = "black",
family = "Times"))
|