This notebook aims to provide a quick introduction to R spatial analysis and cartography.
R is a language dedicated to statitics and data analysis. It has also a lot of strong packages for spatial analysis. Recent packages like {sf} allows easy Simple Features manipulation.
This document is in a writing stage. If you want to contribute, please see the Contact Us webpage
This document aims to complete the R Overview and R Quickstart. If you don't have read them, please consider doing it if you are new to R.
Let's reproduce the study case in the PostGIS quickstart .
We want to represent the sudden infant death syndrome (SIDS) in North Carolina (USA) data from the {spData} package.
More information about the dataset here: nowosad.github.io/spData/reference/nc.sids.html
In [ ]:
library('sf') #SimpleFeature Library to handle shapefiles
library('ggplot2') # Plotting library to create the maps
Package: sf
Version: 0.7-7
Title: Simple Features for R
Description: Support for simple features, a standardized way to
encode spatial vector data. Binds to 'GDAL' for reading and writing
data, to 'GEOS' for geometrical operations, and to 'PROJ' for
projection conversions and datum transformations.
License: GPL-2 | MIT + file LICENSE
URL: https://github.com/r-spatial/sf/, https://r-spatial.github.io/sf/
BugReports: https://github.com/r-spatial/sf/issues/
In [ ]:
options(jupyter.plot_mimetypes = 'image/png')
In [ ]:
sids <- st_read(dsn = "/usr/local/lib/R/site-library/spData/shapes/sids.shp" )
Let's have quick show of the data.
Of the 6 first rows
In [ ]:
head(sids)
In [ ]:
ggplot(sids)+
geom_sf()
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
In [ ]:
sids['sids_rate74'] <- (sids['SID74'] * 1000)/ sids['BIR74']
Let's see if our
In [ ]:
head(sids[,c(1,5,24)])
How does it look like ? Lets add that to our map.
Here we will use several functions and parameters:
In [ ]:
ggplot(sids)+
geom_sf(aes(fill = sids_rate74))+
scale_fill_viridis_c()
In [ ]:
map <- ggplot(sids)+
geom_sf(aes(fill = sids_rate74))+
scale_fill_viridis_c()
map
Adding a title and a subtitle
In [ ]:
map <- map + ggtitle("SIDS in North Carolina", "1974 -1978")
map
Change the legend title and place it below the map
In [ ]:
map <- map + scale_fill_viridis_c(name = "SIDS cases \nfor 1000 births") +
theme(legend.position = "bottom")
map
Remove the labels for x and y axis
In [ ]:
map <- map + theme(axis.title.x = element_blank(),axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
theme(axis.title.y = element_blank(),axis.text.y = element_blank(), axis.ticks.y = element_blank())
map
Some packages are not provided here, but you can use an OpenStreetMap basemap using Leaflet and make it more intractive for example.
Try to reproduce with the data from 1979 to 1984 (hint: use SID79 and BIR79).
There is a lot of documentation regarding R spatial but you might want to take a look at those ressources: