R 创建地区热力图
程序员文章站
2022-07-14 17:37:33
...
一、行政区域数据
地图文件可取自 GADM database (www.gadm.org)。
二、R代码
setwd("D:/gis/shap/CHN_adm_shp")
library("rgdal")
#china_map_adm2 <- readShapePoly("CHN_adm2.shp")
china_map_adm2 <- readOGR("CHN_adm2.shp",use_iconv = TRUE, encoding="UTF-8")
OGR data source with driver: ESRI Shapefile
Source: "CHN_adm2.shp", layer: "CHN_adm2"
with 344 features
It has 14 fields
Integer64 fields read as strings: ID_0 ID_1 ID_2 CCN_2
guangdong_map <- subset(china_map_adm2,NAME_1=="Guangdong")
summary(guangdong_map)
Object of class SpatialPolygonsDataFrame
Coordinates:
min max
x 109.66402 117.3107
y 20.22264 25.5203
Is projected: FALSE
proj4string :
[+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0]
Data attributes:
ID_0 ISO NAME_0 ID_1 NAME_1 ID_2 NAME_2 HASC_2
49:21 CHN:21 China:21 6 :21 Guangdong:21 43 : 1 Chaozhou : 1 NA's:21
1 : 0 Anhui : 0 44 : 1 Dongguan : 1
10 : 0 Beijing : 0 45 : 1 Foshan : 1
11 : 0 Chongqing: 0 46 : 1 Guangzhou: 1
12 : 0 Fujian : 0 47 : 1 Heyuan : 1
13 : 0 Gansu : 0 48 : 1 Huizhou : 1
(Other): 0 (Other) : 0 (Other):15 (Other) :15
CCN_2 CCA_2 TYPE_2 ENGTYPE_2 NL_NAME_2 VARNAME_2
0:21 NA's:21 Dìjíshì :21 Autonomous Prefecture: 0 潮州市 : 1 Cháozhōu : 1
Dìqu : 0 League : 0 东莞市 : 1 Dōngguǎn : 1
Méng : 0 Municipality : 0 佛山市 : 1 Fóshān : 1
Zhíxiáshì: 0 Prefecture : 0 广州市 : 1 Guǎngzhōu: 1
Zìzhìzhou: 0 Prefecture City :21 河源市 : 1 Héyuán : 1
惠州市 : 1 Huìzhōu : 1
(Other):15 (Other) :15
str(guangdong_map,max.level=2)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
aaa@qq.com data :'data.frame': 21 obs. of 14 variables:
aaa@qq.com polygons :List of 21
aaa@qq.com plotOrder : int [1:21] 11 14 10 5 19 18 6 9 7 17 ...
aaa@qq.com bbox : num [1:2, 1:2] 109.7 20.2 117.3 25.5
.. ..- attr(*, "dimnames")=List of 2
aaa@qq.com proj4string:Formal class 'CRS' [package "sp"] with 1 slot
#新增一列id,值为行名称
aaa@qq.com$id <- rownames(aaa@qq.com)
guangdongdata <- aaa@qq.com
head(guangdongdata)
ID_0 ISO NAME_0 ID_1 NAME_1 ID_2 NAME_2 HASC_2 CCN_2 CCA_2 TYPE_2 ENGTYPE_2 NL_NAME_2 VARNAME_2 id zhibiao
42 49 CHN China 6 Guangdong 43 Chaozhou <NA> 0 <NA> Dìjíshì Prefecture City 潮州市 Cháozhōu 42 26.44414
43 49 CHN China 6 Guangdong 44 Dongguan <NA> 0 <NA> Dìjíshì Prefecture City 东莞市 Dōngguǎn 43 17.47148
44 49 CHN China 6 Guangdong 45 Foshan <NA> 0 <NA> Dìjíshì Prefecture City 佛山市 Fóshān 44 18.22510
45 49 CHN China 6 Guangdong 46 Guangzhou <NA> 0 <NA> Dìjíshì Prefecture City 广州市 Guǎngzhōu 45 28.24686
46 49 CHN China 6 Guangdong 47 Heyuan <NA> 0 <NA> Dìjíshì Prefecture City 河源市 Héyuán 46 20.83134
47 49 CHN China 6 Guangdong 48 Huizhou <NA> 0 <NA> Dìjíshì Prefecture City 惠州市 Huìzhōu 47 28.36303
library(broom)
#将空间数据转换为数据框
#guangdongmapdata<- fortify(guangdong_map,region="id")
#guangdongmapdata <- broom::tidy(guangdong_map,region="id")
guangdongmapdata <- broom::tidy(guangdong_map)
Regions defined for each Polygons
dim(guangdongdata)
[1] 21 16
#虚拟一个指标。
guangdongdata$zhibiao<-runif(dim(guangdongdata)[1],10,30)
#将其与几何映射层进行合并:
guangdongnewmapdata<-merge(guangdongmapdata[,c(-4,-5)],guangdongdata[,c("id","ID_2","NL_NAME_2","zhibiao")],by.x="id",by.y="id")
head(guangdongnewmapdata)
id long lat order group ID_2 NL_NAME_2 zhibiao
1 42 116.9841 24.16766 1 42.1 43 潮州市 26.44414
2 42 116.9797 24.16324 2 42.1 43 潮州市 26.44414
3 42 116.9762 24.15879 3 42.1 43 潮州市 26.44414
4 42 116.9694 24.15356 4 42.1 43 潮州市 26.44414
5 42 116.9595 24.15051 5 42.1 43 潮州市 26.44414
6 42 116.9541 24.14843 6 42.1 43 潮州市 26.44414
library("ggplot2")
library("ggthemes")
library("mapproj")
# 绘制地图
ggplot()+
geom_polygon(data=guangdongnewmapdata,aes(x=long,y=lat,group=group,fill=zhibiao),col="grey95")+
scale_fill_gradient(low="white",high="steelblue") +
coord_map("polyconic") +
theme_map()
#添加城市名标签
midpos <- function(x) mean(range(x,na.rm=TRUE))
library("plyr")
centres <- ddply(guangdongnewmapdata,.(NL_NAME_2),colwise(midpos,.(long,lat)))
# centres <- aggregate(cbind(long, lat) ~ NL_NAME_2, data=guangdongnewmapdata,
# FUN=function(x)mean(range(x)))
ggplot(guangdongnewmapdata,aes(long,lat)) +
geom_polygon(aes(group=group,fill=zhibiao),colour="black") +
scale_fill_gradient(low="white",high="steelblue") +
geom_text(aes(long, lat, label=NL_NAME_2),data=centres) +
theme(
panel.grid = element_blank(),
panel.background = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank()
)
参考资料
[1] R笔记2:ggplot绘制商务图表--省分市的热力地图. 刘万祥. http://chuansong.me/n/1448600
[2] R语言可视化——关于ggplot所支持的数据地图素材类型. 杜雨. https://zhuanlan.zhihu.com/p/27360411
[3] Making Maps in R. Kevin Johnson. http://www.kevjohnson.org/making-maps-in-r/
[4] Using R — Working with Geospatial Data (and ggplot2). Bethany Yollin. http://mazamascience.com/WorkingWithData/?p=1494
推荐阅读