查看原文
其他

干货分享 | R_ggplot2地理信息可视化_史上最全(二)

R语言中文社区 全国地研联 2019-06-30


前言:

上文R_ggplot2地理信息可视化_史上最全(一)讲了sp和sf数据类型,这篇讲解地图数据集以及与其他几何对象的结合,还有栅格地图。

注:蓝字表示文末有其网址链接


4.地图数据集


地图数据集常见2中格式:

▪json,包括GeoJSON(文件后缀为.geojson)TopoJson(文件后缀为.json)。

▪shp, shp对象比较特殊,是由很多个文件组成的,通常在同一个文件下还有.shx和.dbf格式的文件。这些文件必须在一起,否则不能成功读取。

▪.rds,这是一种文件格式,分为sp.rds和sf.rds两种,分别对应p和sf两种数据结构。
使用sp::readRDS()读取。

如上图所示,rgdal和sf功能比较全,用得也比较多。
地图集下载网站:

▪GADM,注意该网站中,中国地图不包含台湾。

▪中国县级地图 (见文末)提取码:uomy

▪OpenStreetMap

▪阿里云地图,左上角框框里面选择区域,左下角选择下载格式。

▪geojson.io,在线解析和转换格式。

▪mygeodata converter

▪IGIS Map Converter

推荐使用rmapshaper::ms_simplify()简化地图数据,可以指定简化比例,不然真的很卡,该包使用拓扑学的知识简化多边形,简化后在常规分辨率下根本看不出来差别。
该函数支持json,sp,sf等多种输入对象。
object.size()可以查看数据集的存储大小。


4.1 json格式


4.1.1 rgdal包读取

1##           used (Mb) gc trigger (Mb) max used (Mb)
2## Ncells 901585 48.2 1744096 93.2 1744096 93.2
3## Vcells 1789510 13.7 9804475 74.9 12236244 93.4
4## OGR data source with driver: GeoJSON 
5## Source: "E:\R_input_output\data_input\JSON\GeoJSON\China.geojson", layer: "中国"
6## with 35 features
7## It has 10 fields


4.2 sf包读取


sf包读取中文字符不会乱码。

1##           used (Mb) gc trigger (Mb) max used (Mb)
2## Ncells 1112615 59.5    1744096 93.2  1744096  93.2
3## Vcells 2079841 15.9    9804475 74.9 12236244  93.4
4## Reading layer `涓浗' from data source `E:\R_input_output\data_input\JSON\GeoJSON\China.geojson' using driver `GeoJSON'
5## Simple feature collection with 35 features and 10 fields
6## geometry type:  MULTIPOLYGON
7## dimension:      XY
8## bbox:           xmin: 73.50235 ymin: 3.397162 xmax: 135.0957 ymax: 53.56327
9## epsg (SRID):    4326
10## proj4string:    +proj=longlat +datum=WGS84 +no_defs

4.3 shp格式


4.3.1 rgdal包读取

1##           used (Mb) gc trigger (Mb) max used (Mb)
2## Ncells 1113970 59.5    1744096 93.2  1744096  93.2
3## Vcells 2050141 15.7    9804475 74.9 12236244  93.4
4## OGR data source with driver: ESRI Shapefile 
5## Source: "E:\R_input_output\data_input\全国范围的行政边界和人口密度矢量图\CHN_adm\CHN_adm1.shp", layer: "CHN_adm1"
6## with 32 features
7## It has 16 fields
8## 17647616 bytes
9## 1590376 bytes

4.3.2 sf包读取

1##           used (Mb) gc trigger  (Mb) max used (Mb)
2## Ncells 1349388 72.1    2132915 114.0  2132915  114
3## Vcells 2990656 22.9   15829390 120.8 19786461  151
4## Reading layer `CHN_adm1' from data source `E:\R_input_output\data_input\鍏ㄥ浗鑼冨洿鐨勮鏀胯竟鐣屽拰浜哄彛瀵嗗害鐭㈤噺鍥綷CHN_adm\CHN_adm1.shp' using driver `ESRI Shapefile'
5## Simple feature collection with 32 features and 16 fields
6## geometry type:  MULTIPOLYGON
7## dimension:      XY
8## bbox:           xmin: 73.5577 ymin: 15.78 xmax: 134.7739 ymax: 53.56086
9## epsg (SRID):    4326
10## proj4string:    +proj=longlat +datum=WGS84 +no_defs

4.4 .rds数据格式


4.4.1 sp.rds数据

1##           used (Mb) gc trigger  (Mb) max used (Mb)
2## Ncells 1352847 72.3    2132915 114.0  2132915  114
3## Vcells 3317435 25.4   15829390 120.8 19786461  151
4## [1] "SpatialPolygonsDataFrame"
5## attr(,"package")
6## [1] "sp"

4.4.2 sf.rds数据

1##           used (Mb) gc trigger  (Mb) max used  (Mb)
2## Ncells 1353647 72.3    2710561 144.8  2710561 144.8
3## Vcells 4337426 33.1   33817672 258.1 42185982 321.9
4## [1] "sf"         "data.frame"

5.与其他几何对象结合


5.1 sp对象与其它几何对象结合

1rm(list = ls()); gc() # 清空内存
2library(ggplot2)


1##           used (Mb) gc trigger  (Mb) max used  (Mb)
2## Ncells 1359265 72.6    2710561 144.8  2710561 144.8
3## Vcells 4767536 36.4   26023171 198.6 42185982 321.9


5.1.1 气泡饼图


气泡饼图本来是可以用循环来绘制的。
但已经有现场的包了scatterpie。
里面有2个函数:


geom_scatterpie(), geom_scatterpie_legend()。
由于scatterpie目前不支持sf数据模型,且下载的中国地图数据包经过fortify()转化后,与业务数据没有公共变量,故不能合并数据集,只能用2个数据集绘图。

1##           used (Mb) gc trigger  (Mb) max used  (Mb)
2## Ncells 1359664 72.7    2710561 144.8  2710561 144.8
3## Vcells 4770797 36.4   20818536 158.9 42185982 321.9
4## 'data.frame':    34 obs. of  4 variables:
5##  $ id_numbers: int  1 2 3 4 5 6 7 8 9 10 ...
6##  $ x         : num  85.2 88.4 113.9 96 102.7 ...
7##  $ y         : num  41.1 31.5 44.1 35.7 30.6 ...
8##  $ name      : chr  "新疆维吾尔自治区" "西藏自治区" "内蒙古自治区" "青海省" ...
9## OGR data source with driver: GeoJSON 
10## Source: "E:\R_input_output\data_input\JSON\GeoJSON\China.geojson", layer: "中国"
11## with 35 features
12## It has 10 fields

5.2 sf对象与其它几何对象结合


5.2.1 插入散点图

我们使用的数据来自nycflights13数据包。一个美国民航领域的数据包。
其内含有5个数据集:flights, weather, planes, airports, airlines。


散点图一般坐标列的长度与地图数据集坐标列长度不一样,所以通常在ggplot2中用2个数据集绘图。
首先传递地图数据集,
在散点图的时候,指定新的数据集。
根据图层叠加原理,后添加的图层在表层。
最后进行坐标变换。
这里有个bug,变换为某些地图投影后,叠加的几何对象图层就消失了,只剩地图底层了。

1##           used (Mb) gc trigger  (Mb) max used  (Mb)
2## Ncells 1418752 75.8    2710561 144.8  2710561 144.8
3## Vcells 4685554 35.8   20818536 158.9 42185982 321.9
4## [1] "tbl_df"     "tbl"        "data.frame"
5## Reading layer `cb_2013_us_state_20m' from data source `E:\R_input_output\data_input\cb_2013_us_state_20m\cb_2013_us_state_20m.shp' using driver `ESRI Shapefile'
6## Simple feature collection with 52 features and 9 fields
7## geometry type:  MULTIPOLYGON
8## dimension:      XY
9## bbox:           xmin: -179.1473 ymin: 17.88481 xmax: 179.7785 ymax: 71.35256
10## epsg (SRID):    4269
11## proj4string:    +proj=longlat +datum=NAD83 +no_defs

5.2.2 插入气泡图


很多时候,我们需要每个行政区一个图。
这时候可以合并数据集。
将业务数据与行政区域地图数据合并。然后绘图。


中国各省中心点坐标数据(见文末)提取码:kj2a


1##           used (Mb) gc trigger  (Mb) max used  (Mb)
2## Ncells 1424971 76.2    2710561 144.8  2710561 144.8
3## Vcells 4745575 36.3   20818536 158.9 42185982 321.9
4## 'data.frame':    34 obs. of  4 variables:
5##  $ id_numbers: int  1 2 3 4 5 6 7 8 9 10 ...
6##  $ x         : num  85.2 88.4 113.9 96 102.7 ...
7##  $ y         : num  41.1 31.5 44.1 35.7 30.6 ...
8##  $ name      : chr  "新疆维吾尔自治区" "西藏自治区" "内蒙古自治区" "青海省" ...
9## Reading layer `涓浗' from data source `E:\R_input_output\data_input\JSON\GeoJSON\China.geojson' using driver `GeoJSON'
10## Simple feature collection with 35 features and 10 fields
11## geometry type:  MULTIPOLYGON
12## dimension:      XY
13## bbox:           xmin: 73.50235 ymin: 3.397162 xmax: 135.0957 ymax: 53.56327
14## epsg (SRID):    4326
15## proj4string:    +proj=longlat +datum=WGS84 +no_defs

5.2.3 填充颜色


基于变量给行政区域填充颜色。

1##           used (Mb) gc trigger  (Mb) max used  (Mb)
2## Ncells 1425301 76.2    2710561 144.8  2710561 144.8
3## Vcells 4676860 35.7   20818536 158.9 42185982 321.9
4##           used (Mb) gc trigger  (Mb) max used  (Mb)
5## Ncells 1425323 76.2    2710561 144.8  2710561 144.8
6## Vcells 4676895 35.7   20818536 158.9 42185982 321.9
7## 'data.frame':    34 obs. of  4 variables:
8##  $ id_numbers: int  1 2 3 4 5 6 7 8 9 10 ...
9##  $ x         : num  85.2 88.4 113.9 96 102.7 ...
10##  $ y         : num  41.1 31.5 44.1 35.7 30.6 ...
11##  $ name      : chr  "新疆维吾尔自治区" "西藏自治区" "内蒙古自治区" "青海省" ...
12## Reading layer `涓浗' from data source `E:\R_input_output\data_input\JSON\GeoJSON\China.geojson' using driver `GeoJSON'
13## Simple feature collection with 35 features and 10 fields
14## geometry type:  MULTIPOLYGON
15## dimension:      XY
16## bbox:           xmin: 73.50235 ymin: 3.397162 xmax: 135.0957 ymax: 53.56327
17## epsg (SRID):    4326
18## proj4string:    +proj=longlat +datum=WGS84 +no_defs

5.2.4 插入条形图


因为geom_bar()或geom_col()绘制柱形图,其y坐标代表柱子的高度,而不是纬度。
所有不能之间使用其绘制条形图。
这里使用geom_linerange()函数插入柱形图。
geom_linerange()内有多个映射参数包括:x,ymin, ymax, size。



1##           used (Mb) gc trigger  (Mb) max used  (Mb)
2## Ncells 1432071 76.5    2710561 144.8  2710561 144.8
3## Vcells 4697138 35.9   20818536 158.9 42185982 321.9
4##           used (Mb) gc trigger  (Mb) max used  (Mb)
5## Ncells 1432090 76.5    2710561 144.8  2710561 144.8
6## Vcells 4697168 35.9   20818536 158.9 42185982 321.9
7## 'data.frame':    34 obs. of  4 variables:
8##  $ id_numbers: int  1 2 3 4 5 6 7 8 9 10 ...
9##  $ x         : num  85.2 88.4 113.9 96 102.7 ...
10##  $ y         : num  41.1 31.5 44.1 35.7 30.6 ...
11##  $ name      : chr  "新疆维吾尔自治区" "西藏自治区" "内蒙古自治区" "青海省" ...
12## Reading layer `涓浗' from data source `E:\R_input_output\data_input\JSON\GeoJSON\China.geojson' using driver `GeoJSON'
13## Simple feature collection with 35 features and 10 fields
14## geometry type:  MULTIPOLYGON
15## dimension:      XY
16## bbox:           xmin: 73.50235 ymin: 3.397162 xmax: 135.0957 ymax: 53.56327
17## epsg (SRID):    4326
18## proj4string:    +proj=longlat +datum=WGS84 +no_defs

6.栅格地图


栅格数据可以映射到地图上,但是很多时候变量通常是点数据,如各个气象观测点的气象数据。
这时候需要用插值将点数据插值成栅格数据,再添加到地图底图上。
ggplot2这个实现该功能比较困难。geom_raster()及stat_density_2d()只能使用栅格数据。
这里推荐使用tmap包实现栅格颜色可视化地图。


7.参考资料


geom_map

https://ggplot2.tidyverse.org/reference/geom_map.html

coord_map

https://ggplot2.tidyverse.org/reference/coord_map.html

geom_density_2d

https://ggplot2.tidyverse.org/reference/geom_density_2d.html

geom_tile

https://ggplot2.tidyverse.org/reference/geom_tile.html

利用R语言绘制世界航班路线图

scatterpie离散饼图

https://cran.r-project.org/web/packages/scatterpie/vignettes/scatterpie.html

ggplot2与sf包结合画地图

https://cfss.uchicago.edu/geoviz_plot.html

部分地图投影参考图

https://www.r-bloggers.com/map-projections-in-oce/

Projectionlist源码

https://github.com/zachcp/phylogeo/blob/master/R/utility-functions.R

sf对象详细介绍

https://cran.r-project.org/web/packages/sf/vignettes/sf1.html#what_is_a_feature

rgdal Tips

http://zevross.com/blog/2016/01/13/tips-for-reading-spatial-files-into-r-with-rgdal/

手把手教你使用ggplot2绘制中国地图

weather Visualizatio

https://homepage.divms.uiowa.edu/~luke/classes/STAT4580/weather.html


文中网址

墨卡托投影 

http://desktop.arcgis.com/zh-cn/arcmap/10.3/guide-books/map-projections/mercator.htm

正弦曲线投影

http://desktop.arcgis.com/zh-cn/arcmap/10.3/guide-books/map-projections/sinusoidal.htm

摩尔维特投影

http://desktop.arcgis.com/zh-cn/arcmap/10.3/guide-books/map-projections/mollweide.htm

等距方位投影

http://desktop.arcgis.com/zh-cn/arcmap/10.3/guide-books/map-projections/azimuthal-equidistant.htm

亚尔勃斯等积圆锥投影

http://desktop.arcgis.com/zh-cn/arcmap/10.3/guide-books/map-projections/albers-equal-area-conic.htm

球心投影

http://desktop.arcgis.com/zh-cn/arcmap/10.3/guide-books/map-projections/gnomonic.htm

正射投影

http://desktop.arcgis.com/zh-cn/arcmap/10.3/guide-books/map-projections/orthographic.htm

简单圆锥投影

http://desktop.arcgis.com/zh-cn/arcmap/10.3/guide-books/map-projections/simple-conic.htm

兰勃特等角圆锥投影

http://desktop.arcgis.com/zh-cn/arcmap/10.3/guide-books/map-projections/lambert-conformal-conic.htm

Projection methods

https://proj4.org/operations/projections/index.html

GADM

https://gadm.org/download_country_v3.html

中国县级地图 

https://pan.baidu.com/share/init?surl=JALgWfl7CmlntmujyqtjaQ 提取码:uomy

OpenStreetMap

https://www.openstreetmap.org./#map=1/-70/111

阿里云地图,

http://datav.aliyun.com/tools/atlas/#&lat=33.521903996156105&lng=104.29849999999999&zoom=4

geojson.io

http://geojson.io/#map=7/36.421/118.751

mygeodata converter

https://mygeodata.cloud/converter/shp-to-geojson

IGIS Map Converter

https://map.igismap.com/converter

中国各省中心点坐标数据

https://pan.baidu.com/share/init?surl=tFK7HBjpWF3cJ9M4rJgVZA

 提取码:kj2a


-The End-


    来源 | R语言中文社区公众号   

 排版编辑 | 姜畔

    责任编辑 | 陈诗音

   审核 | 任宇飞  王冠  常贵蒋


猜你喜欢

干货分享 | R_ggplot2地理信息可视化_史上最全(一)

地学快讯 | 2019全国两会城乡规划领域热点前瞻

地学快讯 | 2019全国两会自然资源热点前瞻

地学快讯 | 2019年QS世界大学学科排名出炉!

全国地研联

没时间解释了,快长按左边二维码关注我们~~

觉得不错,请点给地小联点👍哦~

    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存