Introduction to Spatial Analysis in R

  • 前言
  • R语言简介
  • 入门介绍
    • 软件安装
    • 安装包介绍
    • 帮助

前言

本教程是分析R中空间数据的一个简介,特别是通过使用R进行地图制作base图形和R的各种专用地图制作包,如tmapleftlet。它介绍使用R作为快速,用户友好和非常强大的命令行GIS 的基础知识。

如果没有之前没有用过R,可以参考R的入门教程,例如A Student’s Guide to R (Horton,Pruim and Kaplan,2015)或R的官方教程:Efficient R Programming(Gillespie and Lovelace,2016)。

教程分为五个部分:
1. 简介:提供R的语法指南和准备教程
2. R中的空间数据:描述了R中的基本空间函数
3. 创建和操纵空间数据:包括改变投影,裁剪和空间连接
4. 使用tmapggplot2leaflet制作地图:本节演示了更多地图制作的高级可视化工具
5. 进一步用R进行空间分析:汇总相关学习资源。

R语言简介

什么是 R?

  • 是进行数据分析、统计计算和作图的软件环境
  • 是一门能用简短的几行代码完成数据分析的编程语言

为什么选择 R?

  • 免费(开源项目)
  • 广泛使用
    • 全世界超过2百万的用户
    • 功能不断丰富与发展
    • 很多开放的社区资源
  • 很容易的重新运行和调整以前的工作
  • 很好的图形和可视化

其他数据分析软件

SAS, Stata, SPSS, Excel, MATLAB, Minitab, Pandas.

R语言应用前景

R语言的贡献包已经达到了10328个,基本能代替其他大部分的统计软件进行统计分析,当前已实现很多空间分析功能,相关空间分析包仍在github不断开发中。

入门介绍

软件要求

这个教程需要用到R和RStudio软件,最新版下载地址:RRStudio(点击下划线下载)。

安装包介绍

本教程涉及的安装包如下:

  • ggmap: ggplot2的扩展
  • rgdal: R与流行的C / C ++空间数据处理库gdal的接口
  • rgeos: R与强大的矢量数据处理库geos的接口
  • maptools: 提供多种绘图函数
  • dplyr and tidyr: 简化数据处理步骤包
  • tmap: 新开发的快速制图包

代码注释

养成用英文注释代码的习惯,注释方法: # + space + 注释说明,注释位于命令行上方一行命令行之后,如下:

1
2
3
4
# Genarate data
x <- 1:400
y <- sin(x/10) * exp(x * -0.01)
plot(x,y) # plot the result

结果如图1所示:

Figure1-Basic plot of x and y

安装包使用

安装包的使用需要注册并加载,注册过的安装包(以ggplot2为例)可以通过**library(ggplot2)**直接加载。

  • 单个注册与加载

1
2
install.packages("ggplot2") # install
library(ggplot2) # library
  • 批量注册与加载

1
2
3
x <- c("ggmap", "rgdal", "rgeos", "maptools", "dplyr", "tidyr", "tmap")
install.packages(x) # warning: this may take a number of minutes
lapply(x, library, character.only = TRUE) # load the required packages

帮助

在命令行窗口依次输入并分别运行以下三个命令:

1
2
3
help(plot)
?plot
??regression

help(plot)?plot查看函数plot的具体说明
??regression查找与regression有关的库或安装包

References

Lovelace, R., & Cheshire, J. (2014). Introduction to visualising spatial data in R. National Centre for Research
Methods Working Papers, 14(03). Retrieved from https://github.com/Robinlovelace/Creating-maps-in-R
Bivand, R. S., Pebesma, E. J., & Rubio, V. G. (2013). Applied spatial data analysis with R. Springer. 2nd ed.
Cheshire, J., & Lovelace, R. (2015). Spatial data visualisation with R. In C. Brunsdon & A. Singleton (Eds.),
Geocomputation (pp. 1–14). SAGE Publications. Retrieved from https://github.com/geocomPP/sdv . Full
chapter available from https://www.researchgate.net/publication/274697165_Spatial_data_visualisation_with_R
Dorman, M. (2014). Learning R for Geospatial Analysis. Packt Publishing Ltd.
Gillespie, Colin, and Robin Lovelace. 2016. Efficient R Programming: A Practical Guide to Smarter
Programming. O’Reilly Media. https://csgillespie.github.io/efficientR/.
Kabacoff, R. (2011). R in Action. Manning Publications Co.
Lamigueiro, O. P. (2012). solaR: Solar Radiation and Photovoltaic Systems with R. Journal of Statistical
Software, 50(9), 1–32. Retrieved from http://www.jstatsoft.org/v50/i09
Wickham, H. (2014). Tidy data. The Journal of Statistical Software, 14(5), Retrieved from http://www.jstatsoft.org/v59/i10
Wilkinson, L. (2005). The grammar of graphics. Springer.

-------------文章结束啦 ฅ●ω●ฅ 感谢您的阅读-------------