Spatial Data

Spatial data are special, as they describe locations on the surface of the planet Earth. Spatial data come in different sizes and shapes: You may encounter point data (each point can be described by a single latitude and longitude) or areal data, a full description of which will need information about the many points the connections between which are required to form the polygons that are the mathematical equivalent of areals.

In other words, some spatial data may be quite irregular, as in the case of areal data the usual rectangular data matrices will not do the trick.

Spatial data are paramount in the geo-sciences (geodesy, geography etc.). Here, one particular software, ArcGIS has set a quasi-standard for spatial data, so called shapefiles. Luckily, a group of people (led by Roger Bivand, Edzer Pebesma and Virgilio Gómez-Rubio) were kind enough to develop tools that can read such files into R and store them as special objects that belong to special classes. These tools are part of the package maptools, which has to be installed prior to any work with spatial data. Another group of tools is GIStools (by Chris Brunsdon et al.), and there is certainly more. Apologies to all developers whose work is not mentioned here. The CRAN Task View: Spatial Data (new window) provides a comprehensive overview.

Areal data

Areal data are mathematically equivalent to polygons. The following command applies:

library(maptools)
my.spatial.data <- readShapePoly("name-of-shape-file")

Spatial data typically are projected from a flat surface to the earth's surface. Different system for projection are available, and a shapefile is accompanied by a .prj file that informs which projection is used. This file is not read by readShapePoly and therefore the projection has to be indicated in a separate step, as in, e.g.:

proj4string(my.spatial.data) <- "+proj=longlat +datum=WGS84"

The resulting object my.spatial.data belongs to a special class, called SpatialPolygonsDataFrame. This class provides several slots: a data slot, a polygons slot (which may contain very many polygons), a slot indicating the order of the plots (aptly calledplotOrder), a bbox slot which gives the coordinates that describe the box into which all the polygons will fit, and the proj4string slot that indicates the projection used (with value NA if the information is missing).

Note that the output of an inquiry into the structure of such an object (i.e., something like str(my.spatial.data) may be very long, depending on the number of polygons. However, as the names of the slots of such an object are known, you may request information about a single slot only, as in

str(my.spatial.data@data)

© W. Ludwig-Mayerhofer, R Guide | Last update: 16 Jan 2017