The R Spatial Ecosystem

“Historical” Packages

  • rgdal: interface between R and GDAL (Geospatial Data Abstraction Library) and PROJ4 libraries: raster / vector geospatial data formats and coordinate transformation.

  • sp: classes and methods for spatial data in R.

  • rgeos: interface between R and GEOS (Geometry Engine - Open Source) library: area, perimeter, distances, dissolve, buffer, overlap, union, contains…

These packages are still widely used.

Simple Features for R

  • sf Website: Simple Features for R

  • First release: October 20, 2016

  • sp, rgeos and rgdal functionnalities in one package.

  • Easier data handling, simpler objects.

  • Tidy data: compatibility with the pipe synthax and tidyverse operators.

  • Main author and maintainer: Edzer Pebesma (also sp author)


sf objects data structure:

format sf

Using sf

Data Import

Reading layer `martinique' from data source `/home/tim/Documents/prz/flo/lecture/data/mtq/martinique.shp' using driver `ESRI Shapefile'
Simple feature collection with 34 features and 23 fields
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 690574.4 ymin: 1592426 xmax: 736126.5 ymax: 1645660
epsg (SRID):    32620
proj4string:    +proj=utm +zone=20 +datum=WGS84 +units=m +no_defs

Data Display

Default

Only geometry

Distance Matrix

Units: m
          [,1]     [,2]      [,3]      [,4]      [,5]
[1,]     0.000 35297.56  3091.501 12131.617 17136.310
[2,] 35297.557     0.00 38332.602 25518.913 18605.249
[3,]  3091.501 38332.60     0.000 15094.702 20226.198
[4,] 12131.617 25518.91 15094.702     0.000  7177.011
[5,] 17136.310 18605.25 20226.198  7177.011     0.000

Other Packages

CRAN task views aim to provide some guidance which packages on CRAN are relevant for tasks related to a certain topic.

CRAN Task View: Analysis of Spatial Data:

  • Classes for spatial data
  • Handling spatial data
  • Reading and writing spatial data
  • Visualisation
  • Point pattern analysis
  • Geostatistics
  • Disease mapping and areal data analysis
  • Spatial regression
  • Ecological analysis

Maps with R

Overview

Several solutions are available:

  • ggplot2 users can have a look to ggplot2 mapping features (geom_sf) that can mix nicely with ggspatial.
  • For more advanced mapping features in a ggplot2-like syntax have a look to tmap
  • cartography is based on base graphics and allow most of basic and advanced cartographic representations.
    Full disclosure: one of the speakers is the maintainer of cartography.

Here we will focus on cartography and do small examples with ggplot2.

cartography

Choropleth Map

We could create the same map on a cartogram based on the active population stock.

Gridded Map

These maps may allow to hide or, at least, diminish the MAUP.

It is also possible to create hexagonal grids.

Smoothed Map

smoothLayer() uses functions from package SpatialPosition to computes Stewart’s potentials of population.
The computation of potentials could be considered as a spatial interpolation method such as inverse distance weighted interpolation (IDW) or kernel density estimator. These models aim to estimate unknown values of non-observed points from known values given by measure points. Cartographically speaking, they are often used to get a continuous surface from a set of discrete points. However, Stewart model is mainly a spatial interaction modeling approach, with a possible secondary use for spatial interpolation.

Cheat Sheet

The cheat sheet displays a quick overview of cartography’s main features:

cartography cheat sheet

Exploration of SIRENE & OSM databases for restaurants

Interactive Visualisation

mapview allows highly customizable maps:

These maps, as appealing as they seem to be, are not really suitable for presenting geostatistical information.

Nonetheless, they can be really useful for exploratory data analysis.

Count Points in Units

In a regular grid

Create a small function that builds a regular grid and counts points in each cell.

Create a function that plots a map

Build grids and display maps

Smooth Density Analysis

Define a function that computes smoothed restaurant density (KDE method).

Define a function that maps the results

reproducibility

Always share your R and packages configuration !

R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.7.0
LAPACK: /usr/lib/lapack/liblapack.so.3.7.0

locale:
 [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8    
 [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8   
 [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] maptools_0.9-3        raster_2.6-7          sp_1.3-1             
 [4] spatstat_1.56-1       rpart_4.1-13          nlme_3.1-137         
 [7] spatstat.data_1.3-1   mapview_2.6.0         ggplot2_3.0.0        
[10] SpatialPosition_1.2.0 cartogram_0.1.0       cartography_2.1.2    
[13] dplyr_0.7.6           sf_0.6-3              rmdformats_0.3.3     
[16] knitr_1.20           

loaded via a namespace (and not attached):
 [1] jsonlite_1.5         viridisLite_0.3.0    shiny_1.1.0         
 [4] assertthat_0.2.0     highr_0.7            stats4_3.5.1        
 [7] yaml_2.2.0           pillar_1.3.0         backports_1.1.2     
[10] lattice_0.20-35      glue_1.3.0           digest_0.6.17       
[13] promises_1.0.1       polyclip_1.9-1       colorspace_1.3-2    
[16] Matrix_1.2-14        htmltools_0.3.6      httpuv_1.4.5        
[19] plyr_1.8.4           pkgconfig_2.0.2      questionr_0.6.3     
[22] bookdown_0.7         purrr_0.2.5          xtable_1.8-3        
[25] scales_1.0.0         webshot_0.5.0        tensor_1.5          
[28] satellite_1.0.1      later_0.7.4          spatstat.utils_1.9-0
[31] tibble_1.4.2         mgcv_1.8-24          withr_2.1.2         
[34] lazyeval_0.2.1       deldir_0.1-15        magrittr_1.5        
[37] crayon_1.3.4         mime_0.5             evaluate_0.11       
[40] foreign_0.8-70       class_7.3-14         tools_3.5.1         
[43] stringr_1.3.1        munsell_0.5.0        bindrcpp_0.2.2      
[46] compiler_3.5.1       e1071_1.7-0          rlang_0.2.2         
[49] classInt_0.2-3       units_0.6-0          grid_3.5.1          
[52] rstudioapi_0.7       goftest_1.1-1        htmlwidgets_1.2     
[55] crosstalk_1.0.0      miniUI_0.1.1.1       base64enc_0.1-3     
[58] labeling_0.3         rmarkdown_1.10       gtable_0.2.0        
[61] codetools_0.2-15     abind_1.4-5          DBI_1.0.0           
[64] R6_2.2.2             rgdal_1.3-4          rgeos_0.3-28        
[67] bindr_0.1.1          rprojroot_1.3-2      stringi_1.2.4       
[70] Rcpp_0.12.18         png_0.1-7            leaflet_2.0.2       
[73] spData_0.2.9.4       tidyselect_0.2.4     xfun_0.3