The course will cover mapping and spatial analyses using R. Most of the features of Arcgis or other GIS software are available in R: some are missing, but R also opens more possibilities by making full data accessible. The main topics will be the use of shape files and raster maps, including the creation of maps as well as spatial analyses.
The class will start with a two-week review of basic R. Participants who have reasonable experience with R might decide to skip these. These will cover data types in R, use of dataframes, and basic graphing functions in base R and how they are used for creating maps.
During each session, I will explain methods and present examples of their use, then students will work on assignments using these methods. Datasets will be provided, but students are encouraged to bring their own geographic data as well. A course web site will provide sample code, data, and a list of key R functions.
Students should
- have their own computer to work on during the sessions,
- have R and several packages already installed and working,
- know their computer's file system (ie, how to find files by typing the path plus file name),
Schedule
- Time: Each Wednesday, 11:00-1:00PM (except 27 Sept, when it will run noon-2:PM)
- Dates: 27 September - 15 November
- Where: Conference room
Software required
Web site at http://richardcondit.org/workshops/Rgis/MortonArboretum including:
Each will be updated regularly throughout the course.
Course syllabus
Basic R: Data handling [week 1]
- Data types
- Numeric
- Character (surrounded by quotes)
- Boolean
- Objects
- Atomic (or scalar): a single value
- Vector: many values
- Dataframe: table
- * List [*not necessary for basics]
- * Matrix, Array [*not necessary for basics]
- Assigning objects and variables
- Vectorizing calculations
- Filtering
- Installing packages
- Reading external data
Programming R [week 2]
- Writing functions
- Define a function: source
- Function arguments, defaults
- Declaring empty vectors
- Loops
- Debugging
- Return values
- Scope of variables
- Saving R objects
Maps with R's base package [week 3]
- Basic R graphing
- Plotting a dataframe with a line
- Basic cosmetics
- Color: col
- Point character: pch
- Point size: cex
- Axis: xlab, ylab, axes
- Structuring multiple polygons in a list
- Polygon vs. line
- UTM coordinates
GIS tools in R [week 4]
- Packages sp, maptools, rgdal
- Classes (type S4)
- SpatialPolygonsDataFrame
- SpatialPolygons
- Polygons
- Polygon
- Etc. for Lines, Points
- Subsetting the data
- Bounding box
- Maps
- Area
GIS rasters in R [week 5]
- World elevation maps
- Package raster
- Extracting the data
Spatial analyses [week 6]
- Packages splancs, sgeostat, spatstat
- Coordinate transformation
- Distance to point or line
Spatial analyses [week 7]
- Coordinate transformation (review)
- Point in polygon (review)
- Distance to point or line
- Nearest neighbor
GPSVisualizer [week 8]
- Create maps with world coordinates and GPS Visualizer
- Show the map on Google
- Create R spatial classes
Principal R functions
- Basic R
- table or vector size: length, dim, nrow, ncol
- view a few rows: head, tail
- create vectors: c, seq, rep, sample, sample.int
- declare vectors: numeric, character
- load data: read.delim, read.table, load, attach
- save work: write.table, save, savehistory
- Data exploration
- math: log, exp, sin, cos, etc
- stats: sum, mean, median, sd, var, range, min, max
- unique values: unique
- filter: subset
- calculations on table: apply, rowSums, colSums, etc
- grouping: table, tapply
- random numbers: runif, rnorm, rbinom, etc
- Writing functions
- execute from file: source
- definition: function
- looping: for
- finishing: return
- debugging: debug, browser
- Basic mapping
- start a graph: plot
- add points to graph: points
- add lines to graph: line
- fill a polygon: polygon
- open a new graphics window: X11
- switch between graphics windows: dev.set
- add a straight line: abline
- add all axes without labels: box
- add one axis with specified ticks and labels: axis(at=__,lab=__)
- label points: identify
- set graphics parameters: par
- add legend: legend with many options (see help)
- add text: text, mtext
- arguments to plot
- point type: pch
- point size: cex
- color: col
- line thickness: lwd
- line type: lty 'dashed' or 'solid' or 'dotted'
- without axes: axes=FALSE
- title at top: main='yourtitle'
- arguments to par (look at par help for many more)
- multiple plots in a grid: mfcol
- plot size in inches: pin
- label and axis sizes: cex.lab, cex.axis
- tighten label and axis: mgp
- extend labels or lines off graph: xpd
- R GIS
- read shape files: readShapePoly, readShapeLines, readShapePoints
- mapping: sp::plot
- subsetting subset
- create class Polygons: Polygons
- create class Polygon Polygon
- mapping: sp::plot
- set projection CRS (requires sp)
- change projection spTransform (requires sp)
- R Raster
- read geotiff files: raster
- graphing plot, contour
- define a subrectangle: extent
- extract the extent: zoom, crop
- get the data: xyFromCell
- change projection: projectRaster
- Spatial analyses
- Distances: nndistF, dsquare
- Points within polygon point.in.polygon, inout, crop
- Creating a polygon and spatial classes
- One polygon: Polygon etc for Lines, Points
- Several polygons Polygons etc
- The full spatial class SpatialPolygons,SpatialPolygonsDataFrame etc