R as a command line GIS

Robin Lovelace

March 26, 2015

Welcome / Bienvenido / Benvinguda

Introduction

This course is brought to you the Consumer Data Research Centre (CDRC), a project based at the University of Leeds and UCL. It is funded by the ESRC’s (Big Data Network).

The course is based on “Introduction to visualising spatial data in R” (Lovelace and Cheshire 2014), a free, open source and evolving teaching resource. See github.com/Robinlovelace/Creating-maps-in-R.

Please contribute!

Course agenda

COFFEE

A bit about R

Why R?

R is up and coming I

scholar-searches1

Source: r4stats.com

II - Increasing popularity in academia

scholar-searches2

Source: r4stats.com

III - R vs Python

Source: Hadley Wickham. Further information: see “R beats Python! R beats Julia! Anyone else wanna challenge R?” (Matloff 2014)

IV - employment market

jobs

Source: revolution analytics

Why R for spatial data?

“With the advent of “modern” GIS software, most people want to point and click their way through life. That’s good, but there is a tremendous amount of flexibility and power waiting for you with the command line. Many times you can do something on the command line in a fraction of the time you can do it with a GUI (Sherman 2008, p. 283)

Why R for spatial data II

If you get stuck? Just ask! E.g. stackoverflow.com/q/29066198/1694378:

@robinlovelace says:

I want to load only only the LineString, so this is what I do:

library(RCurl)
obj <- getURL("https://raw.githubusercontent.com/Robinlovelace/stplanr/master/inst/extdata/route_data.geojson")
writeLines(obj, "/tmp/obj.geojson")
obj <- readLines("/tmp/obj.geojson")
just_lines <- obj[14:(length(obj) - 28)]
just_lines[1] <- paste0("{",  just_lines[1])
just_lines[length(just_lines)] <- "}"
writeLines(just_lines, "/tmp/just_lines.geojson")

How do I do this properly?

@geospacedman:

Read the data using jsonlite direct from the URL…

 obj <- jsonlite::fromJSON("https://raw.githubusercontent.com/Robinlovelace/stplanr/master/inst/extdata/route_data.geojson")

Convert the first object in the collection to SpatialLines:

sl = SpatialLines(list(Lines(list(Line(obj$features[1,]$geometry$coordinates[[1]])),ID=1)))

Visualisation

Why focus on visualisation?

If you cannot visualise your data, it is very difficult to understand your data. Conversely, visualisation will greatly aid in communicating your results.

Human beings are remarkably adept at discerning relationships from visual representations. A well-crafted graph can help you make meaningful comparisons among thousands of pieces of information, extracting patterns not easily found through other methods. … Data analysts need to look at their data, and this is one area where R shines. (Kabacoff, 2009, p. 45).

Maps, the ‘base graphics’ way

base graphics

Source: Cheshire and Lovelace (2014) - available online

The ‘ggplot2’ way (source: This tutorial!)

Source: This tutorial!

R in the wild 1: Maps of all census variables for local authorities

census

R in the wild 2: Global shipping routes in the late 1700s

Source: R-Bloggers

R in the wild 3: The national propensity to cycle tool (NPCT)

See https://github.com/npct/pct-shiny

R in the wild 4: Mapping bike crashes in Yorkshire I (Lovelace et al. 2015)

Source: Lovelace, Roberts and Kellar (2015)

R vs QGIS (Lovelace et al. 2015)

R in the wild 5: Infographic of housing project finances

grid.raster(readPNG("~/repos/robinlovelace.github.io/figure/unnamed-chunk-53.png"))

Flexibility of ggplot2 - see robinlovelace.net

Getting up-and-running for the tutorial

Before progressing further: Any questions?

Course materials are all available online from a GitHub repository. Click “Download ZIP” to download all the test data, ready to procede.

The main document to accompany this tutorial is a pdf within the main repository. This is to be made available for free worldwide - any comments/corrections welcome.

Further R resources

References

Bivand, Roger S, Edzer J Pebesma, and Virgilio Gómez-Rubio. 2013. Applied spatial data analysis with R. Vol. 747248717. Springer.

Cheshire, James, and Robin Lovelace. 2015. “Spatial data visualisation with R.” In Geocomputation, edited by Chris Brunsdon and Alex Singleton, 1–14. SAGE Publications. https://github.com/geocomPP/sdv.

Ihaka, Ross, and R Gentleman. 1996. “R: a language for data analysis and graphics.” Journal of Computational and Graphical … 5 (3): 299–314. http://www.tandfonline.com/doi/abs/10.1080/10618600.1996.10474713.

Lovelace, Robin, and James Cheshire. 2014. “Introduction to visualising spatial data in R.” National Centre for Research Methods Working Papers 14 (03). London: National Centre for Research Methods; EloGeo. https://github.com/Robinlovelace/Creating-maps-in-R.