OSGeoLive-Notebooks/R/R_spatial_introduction.ipynb

302 lines
8.3 KiB
Plaintext
Executable File

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# R spatial introduction\n",
"\n",
"This notebook aims to provide a quick introduction to R spatial analysis and cartography.\n",
"\n",
"R is a language dedicated to statitics and data analysis. It has also a lot of strong packages for spatial analysis.\n",
"Recent packages like {sf} allows easy [Simple Features](https://en.wikipedia.org/wiki/Simple_Features) manipulation.\n",
"\n",
"This document is in a **writing stage**. If you want to contribute, please see the [Contact Us webpage](https://live.osgeo.org/en/contact.html)\n",
"\n",
"This document aims to complete the [R Overview](http://localhost/osgeolive/en/overview/R_overview.html) and [R Quickstart](http://localhost/osgeolive/en/quickstart/R_quickstart.html). If you don't have read them, please consider doing it if you are new to R."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simple mapping\n",
"\n",
"Let's reproduce the study case in the [PostGIS quickstart ](http://localhost/osgeolive/en/quickstart/postgis_quickstart.html).\n",
"\n",
"We want to represent the sudden infant death syndrome (SIDS) in North Carolina (USA) data from the {spData} package.\n",
"\n",
"More information about the dataset here:\n",
"[nowosad.github.io/spData/reference/nc.sids.html](https://nowosad.github.io/spData/reference/nc.sids.html)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Needed libaries\n",
"\n",
"R comes with a lot of packages to provides functionnalities. Those functions are stocked in libraries and in order to call them, you need to load them beforehand. To do so use the function *library(< package name >)* "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"library('sf') #SimpleFeature Library to handle shapefiles\n",
"library('ggplot2') # Plotting library to create the maps"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Package: sf\n",
" Version: 0.9-5\n",
" Title: Simple Features for R\n",
" Description: Support for simple features, a standardized way to\n",
" encode spatial vector data. Binds to 'GDAL' for reading and writing\n",
" data, to 'GEOS' for geometrical operations, and to 'PROJ' for\n",
" projection conversions and datum transformations.\n",
" License: GPL-2 | MIT + file LICENSE\n",
" URL: https://github.com/r-spatial/sf/, https://r-spatial.github.io/sf/\n",
" BugReports: https://github.com/r-spatial/sf/issues/\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"options(jupyter.plot_mimetypes = 'image/png')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Loading the data\n",
"\n",
"We need to load the sids data that came from the *sids.shp* file. To do so, we'll use the function *sf::st_read()*."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sids <- st_read(dsn = \"/usr/lib/R/site-library/spData/shapes/sids.shp\" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's have quick show of the data.\n",
"\n",
"Of the 6 first rows"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"head(sids)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mapping\n",
"#### A basic Map\n",
"Let's see what it looks :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ggplot(sids)+\n",
" geom_sf()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we want to represent the rate of sids for the 1000 birth in the 1974 and 1978 period, we will use the data from the BIR74 and SID74 columns. In the quickstart, they represent counts with colors, as we don't want to offence geographers, lets use a ratio instead. So we want to create a new column with the "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sids['sids_rate74'] <- (sids['SID74'] * 1000)/ sids['BIR74']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see if our "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"head(sids[,c(1,5,24)])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How does it look like ? Lets add that to our map.\n",
"\n",
"Here we will use several functions and parameters:\n",
"\n",
"* ***ggplot(sids)*** -> we want to make plot of the SIDS dataset\n",
"* ***geom_sf(aes(fill = sids_rate74))*** -> we want to apply aestetics to the filling of the geometry using the data from sids_rate74 column\n",
"* ***scale_fill_viridis_c()*** -> with the viridis color scale dedicated to filling for continuous data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ggplot(sids)+\n",
" geom_sf(aes(fill = sids_rate74))+\n",
" scale_fill_viridis_c()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Not bad. Now we need some refinements like a title, some labels.\n",
"Those functions are provided by ggplot.\n",
"\n",
"### Making a better map\n",
"\n",
"First we should save the map in an object so we won't have to write all the code each time"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"map <- ggplot(sids)+\n",
" geom_sf(aes(fill = sids_rate74))+\n",
" scale_fill_viridis_c()\n",
"\n",
"map"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Adding a title and a subtitle"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"map <- map + ggtitle(\"SIDS in North Carolina\", \"1974 -1978\")\n",
"map"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Change the legend title and place it below the map"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"map <- map + scale_fill_viridis_c(name = \"SIDS cases \\nfor 1000 births\") + \n",
" theme(legend.position = \"bottom\")\n",
"map"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remove the labels for x and y axis"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"map <- map + theme(axis.title.x = element_blank(),axis.text.x = element_blank(), axis.ticks.x = element_blank()) +\n",
" theme(axis.title.y = element_blank(),axis.text.y = element_blank(), axis.ticks.y = element_blank())\n",
"map"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What's next ?\n",
"\n",
"Some packages are not provided here, but you can use an OpenStreetMap basemap using Leaflet and make it more intractive for example.\n",
"\n",
"Try to reproduce with the data from 1979 to 1984 (*hint*: use SID79 and BIR79).\n",
"\n",
"There is a lot of documentation regarding R spatial but you might want to take a look at those ressources:\n",
"\n",
"* [Geocomputation with R](https://geocompr.robinlovelace.net/) by Robin Lovelace, Jakub Nowosad, Jannes Muenchow\n",
"* [R Spatial](http://www.rspatial.org/) by Edzer Pebesma\n",
"* [Introduction to mapping with {sf} & Co. ](https://statnmap.com/2018-07-14-introduction-to-mapping-with-sf-and-co/) on spatial analysis with R by Sebastien Rochette"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}