34
Dr Nick Bearman & Dr Robin Lovelace Wed 24 th April 9am – 10:45am @nickbearmanuk Spatial data with R: an introduction Wifi: ‘eduroam’ or ‘Wifi Guest’ Install R https://cran.r-project.org/ Install RStudio https://www.rstudio.com/ install.packages(“sf”) install.packages(“tmap”) If it is not working, ask! http://bit.ly/GISRUK-2019

Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Dr Nick Bearman & Dr Robin LovelaceWed 24th April9am – 10:45am @nickbearmanuk

Spatial data with R: an introduction

Wifi: ‘eduroam’ or ‘Wifi Guest’

Install R https://cran.r-project.org/

Install RStudio https://www.rstudio.com/

install.packages(“sf”)

install.packages(“tmap”)

If it is not working, ask!

http://bit.ly/GISRUK-2019

Page 2: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

• Log on!

• Toilets

• Fire Alarm

• Presentations, handouts and data online

Housekeeping

http://bit.ly/GISRUK-2019

Page 3: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

R as a GIS

• Command line driven, rather than GUI

• Disadvantages

– Remembering commands

• glossary

– Steeper

learning

curve

Page 4: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

R as a GIS

• Advantages

– Easy to record what you did and repeat

specific pieces of work

– Lots of reproducible examples on the web

– Easily scriptable.

– 134,567 maps? Easy! (354 areas X ~392 variables)

– 2011 Census Open Atlas

– http://www.alex-singleton.com/r/2014/02/05/2011-census-open-atlas-project-version-two/

Page 5: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

R in Action

Economically active – Full-time students

Page 6: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Age structure – 18 to 19 Age structure – 20 to 24

Page 7: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Single Married

Page 8: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Ethnic group: white

Page 9: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

R as a GISTopography

http://topography.geotheory.co.uk/

Page 10: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

https://gist.github.com/halhen/659780120accd82e043986c8b57deae

0

Page 11: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

https://gist.github.com/halhen/659780120accd82e043986c8b57deae0

Page 12: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Other GIS software?

• R is very different to ArcGIS, but can do many of

the same operations

• R is free (as is QGIS)

• R is increasingly popular in academic sector

• Try data in QGIS if you like

All can be useful

Page 13: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

This is the console where you can type in commands

Here will show either your files

(the files tab) or your plots (the

plots tab)

This lists the variables you have

Page 14: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Working Directory

• R uses a ‘working directory’ to store your

files in

• You might have a different one for each

project / piece of work

• e.g. M:\Documents\GIS

setwd(“M:/Documents/GIS”)

Page 15: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Variables

R uses variables to

store information –

listed in your ‘workspace’ (top-right)

house.prices <- c(120,150,212,

99,199,299,159)

Page 16: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Variables and Assignments

house.prices

<-

c(120,150,212,99,199,299,159)

Page 17: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Variables and Assignments

house.prices

<-

c(120,150,212,99,199,299,159)

Page 18: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Variables and Assignments

house.prices

<-

c(120,150,212,99,199,299,159)

Page 19: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Variables and Assignment

sthelens

<-

st_read("sthelens.shp")

Page 20: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Variables and Assignment

sthelens

<-

st_read("sthelens.shp")

Page 21: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Variables and Assignment

sthelens

<-

st_read("sthelens.shp")

Page 22: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Variables and Assignment

sthelens

<-

st_read("sthelens.shp")

Page 23: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Variables and Assignment

sthelens

<-

st_read("sthelens.shp")

• Case sensitiveStHelens ≠ sthelens ≠ STHELENS

Page 24: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Data Formats

• Data frames are like a table or spreadsheet• dataframe[rows,cols]

• dataframe[1,] first row

• dataframe[,1] first col

Page 25: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Practical Approach

• (Primarily) self-led practical

• Good to explore the options

• Ask questions as we go through

• Try things

• Use the help ?command

• You will need to install the libraries

• install.packages(“sf”)

• library(sf)

Page 26: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record
Page 27: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record
Page 28: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record
Page 29: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record
Page 30: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

R Scripts

• Why use scripts?

– Easier to correct code

– Can easily re run sections of code, or all code

if you need to start again

– Easy to share code

Page 31: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

• Link to survey:

This is the console where you can type in commands

Here will show either your files

(the files tab) or your plots (the

plots tab)

This lists the variables you haveThis is where you can write scripts

Page 32: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record
Page 33: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Load R

• You will need to install the libraries

• install.packages(“sf”)

• library(sf)

Bit.ly/GISRUK-2019

Page 34: Spatial data with R: an introduction - Geospatial Training Solutionsgeospatialtrainingsolutions.co.uk/data/2019-04-24-GISRUK/presentati… · R as a GIS •Advantages –Easy to record

Workbook.pdfBit.ly/GISRUK-2019