Chapter 7

Displaying Spatial Data: Introduction

Spatial data (also known as geospatial data) are directly or indirectly referenced to a location on the surface of the Earth. Their spatial reference is composed of coordinate values and a system of reference for these coordinates. Spatial data are often accessed, manipulated, or analyzed through Geographic Information Systems (GIS).

Real objects represented by GIS data can be divided into two abstractions: discrete objects (e.g., a road or a river) represented with vector data (points, lines, and polygons), and continuous fields (such as elevation or solar radiation) represented with raster data. The sp package is the preferred option to use vector data in R, and the raster package is the choice for raster data1.

This part exposes several examples where vector and raster data are displayed to show geographic location of features and physical landscape features of a place (reference and physical maps, Chapter 9) or a specific variable in the context of a geographic reference (thematic maps, Chapter 8). These examples make use of several datasets (available at the book website) described in Chapter 10.

7.1 Packages

The CRAN Tasks View “Analysis of Spatial Data”2 summarizes the packages for reading, vizualizing, and analyzing spatial data. This section provides a brief introduction to sp, raster, rasterVis, maptools, rgdal, gstat, and maps. Most of the information has been extracted from their vignettes, webpages, and help pages. You should read them for detailed information.

7.1.1 sp

The sp package (E. J. Pebesma and R. S. Bivand 2005) provides classes and methods for dealing with spatial data in R. The spatial data classes implemented are points (SpatialPoints), grids (SpatialPixels and SpatialGrid), lines (Line, Lines and SpatialLines), rings, and polygons (Polygon, Polygons, and SpatialPolygons), each of them without data or with data (for example, SpatialPointsDataFrame or SpatialLinesDataFrame).

Selecting, retrieving, or replacing certain attributes in spatial objects with data is done using standard methods:

  • [ selects rows (items) and columns in the data.frame.
  • [[ selects a column from the data.frame
  • [[<- assigns or replaces values to a column in the data.frame.

A number of spatial methods are available for the classes in sp:

  • coordinates(object) <- value sets spatial coordinates to create spatial data. It promotes a data.frame into a SpatialPointsDataFrame. value may be specified by a formula, a character vector, or a numeric matrix or data.frame with the actual coordinates.
  • coordinates(object, ...) returns a matrix with the spatial coordinates. If used with SpatialPolygons it returns a matrix with the centroids of the polygons.
  • bbox returns a matrix with the coordinates bounding box.
  • proj4string(object) and proj4string(object) <- value retrieve or set projection attributes on spatial classes.
  • spTransform transforms from one coordinate reference system (geographic projection) to another (requires package rgdal).
  • spplot plots attributes combined with spatial data: Points, lines, grids, polygons.

7.1.2 raster

The raster package (R. J. Hijmans 2013) has functions for creating, reading, manipulating, and writing raster data. The package provides general raster data manipulation functions. The package also implements raster algebra and most functions for raster data manipulation that are common in Geographic Information Systems (GIS).

The raster package can work with raster datasets stored on disk if they are too large to be loaded into memory. The package can work with large files because the objects it creates from these files only contain information about the structure of the data, such as the number of rows and columns, the spatial extent, and the filename, but it does not attempt to read all the cell values in memory. In computations with these objects, the data are processed in chunks.

The package defines a number of S4 classes. RasterLayer, RasterBrick, and RasterStack are the most important:

  • A RasterLayer object represents single-layer (variable) raster data. It can be created with the function raster. This function is able to create a RasterLayer from another object, including another Raster* object, or from a SpatialPixels* and SpatialGrid* object, or even a matrix. In addition, it can create a RasterLayer reading data from a file. The raster package can use raster files in several formats, some of them via the rgdal package. Supported formats for reading include GeoTIFF, ESRI, ENVI, and ERDAS.
  • RasterBrick and RasterStack are classes for multilayer data. A RasterStack is a list of RasterLayer objects with the same spatial extent and resolution. It can be formed with a collection of files in different locations or even mixed with RasterLayer objects that only exist in memory. A RasterBrick is truly a multilayered object, and processing it can be more efficient than processing a RasterStack representing the same data.

The raster package defines a number of methods for raster algebra with Raster* objects: arithmetic operators, logical operators, and functions such as abs, round, ceiling, floor, trunc, sqrt, log, log10, exp, cos, sin, max, min, range, prod, sum, any, and all. In these functions, Raster* objects can be mixed with numbers.

There are several functions to modify the content or the spatial extent of Raster* objects, or to combine Raster* objects:

  • The crop function takes a geographic subset of a larger Raster* object. trim crops a RasterLayer by removing the outer rows and columns that only contain NA values. extend adds new rows and/or columns with NA values.
  • The merge function merges two or more Raster* objects into a single new object.
  • projectRaster transforms values of a Raster* object to a new object with a different coordinate reference system.
  • With overlay, multiple Raster* objects can be combined (for example, multiply them).
  • mask removes all values from one layer that are NA in another layer, and cover combines two layers by taking the values of the first layer except where these are NA.
  • calc computes a function for a Raster* object. With RasterLayer objects, another RasterLayer is returned. With multilayer objects the result depends on the function: With a summary function (sum, max, etc.), calc returns a RasterLayer object, and a RasterBrick object otherwise.
  • stackApply computes summary layers for subsets of a RasterStack or RasterBrick.
  • cut and reclassify replace ranges of values with single values.
  • zonal computes zonal statistics, that is, summarizes a Raster* object using zones (areas with the same integer number) defined by another RasterLayer.

7.1.3 rasterVis

The rasterVis package (Oscar Perpiñán and R. Hijmans 2013) complements the raster package, providing a set of methods for enhanced visualization and interaction. This package defines visualization methods (levelplot) for quantitative data and categorical data, both for univariate and multivariate rasters.

It also includes several methods in the frame of the Exploratory Data Analysis approach: scatterplots with xyplot, histograms and density plots with histogram and densityplot, violin and boxplots with bwplot, and a matrix of scatterplots with splom.

On the other hand, this package is able to display vector fields using arrows, vectorplot, or with streamlines (Wegenkittl and Gröller 1997), streamplot. In this last method, for each point, droplet, of a jittered regular grid, a short streamline portion, streamlet, is calculated by integrating the underlying vector field at that point. The main color of each streamlet indicates local vector magnitude (slope). Streamlets are composed of points whose sizes, positions, and color degradation encode the local vector direction (aspect).

7.1.4 maptools

The maptools package (R. Bivand and Lewin-Koh 2013) provides a set of tools for manipulating and reading geographic data, in particular ESRI (Environmental Systems Research Institute) shapefiles. The package also provides interface wrappers for exchanging spatial objects with packages such as PBSmapping, spatstat, maps, RArcInfo, Stata tmap, WinBUGS, Mondrian, and others. The main functions in the context of this book are

  • readShapePoints reads data from a points shapefile into a SpatialPointsDataFrame object.
  • writePointsShape writes data from a SpatialPointsDataFrame object to a shapefile.
  • readShapeLines reads data from a line shapefile into a SpatialLinesDataFrame object.
  • writeLinesShape writes data from a SpatialLinesDataFrame object to a shapefile.
  • readShapePoly reads data from a polygon shapefile into a SpatialPolygonsDataFrame object.
  • writePolyShape writes data from a SpatialPolygonsDataFrame object to a shapefile.
  • map2SpatialPolygons and map2SpatialLines may be used to convert map objects returned by the map function in the maps package to the classes defined in the sp package.
  • spCbind provides cbind-like methods for Spatial*DataFrame and data.frame objects.

The topology operations on geometries performed by this package (for example, unionSpatialPolygons) use the package rgeos, an interface to the Geometry Engine Open Source (GEOS)3.

7.1.5 rgdal

The rgdal package (R. Bivand, Keitt, and Rowlingson 2013) provides bindings to the Geospatial Data Abstraction Library (GDAL)4. With readOGR and readGDAL, both GDAL raster and OGR vector map data can be imported into R, and GDAL raster data and OGR vector data can be exported with writeGDAL and writeOGR.

In addition, this package provides access to projection and transformation operations from the PROJ.4 library5. This package implements several spTransform methods providing transformation between datums and conversion between projections using PROJ.4 projection arguments.

7.1.6 gstat

The gstat package (E. J. Pebesma 2004) provides functions for geostatistical modeling, prediction, and simulation, including variogram modeling and simple, ordinary, universal, and external drift kriging.

Most of the functionality of this package is beyond the scope of this book. However, some functions must be mentioned:

  • variogram calculates the sample variogram from data, or for the residuals if a linear model is given. vgm generates a variogram and fit.variogram fit ranges and/or sills from a variogram model to a sample variogram.
  • krige is the function for simple, ordinary or universal kriging. gstat is the function for univariate or multivariate geostatistical prediction.

7.1.7 maps

The maps (Becker, Wilks, Brownrigg, and Minka 2013), mapdata (Becker, Wilks, and Brownrigg 2013), and mapproj (McIlroy et al. 2013) packages are useful to draw or create geographical maps. mapdata contains higher resolution databases, and mapproj converts latitude/longitude coordinates into projected coordinates.

7.2 Further Reading

  • (Slocum 2005) and (Dent, Torguson, and Hodler 2008) are comprehensive books on thematic cartography and geovisualization. They include chapters devoted to data classification, scales, map projections, color theory, typography, and proportional symbol, choropleth, dasymetric, isarithmic, and multivariate mapping. Several resources are available at their accompanying websites6.
  • (R. S. Bivand, E. J. Pebesma, and Gomez-Rubio 2008) is the essential reference to work with spatial data in R. R. Bivand and E. Pebesma are the authors of the fundamental sp package, and they are the authors or maintainers of several important packages such as gstat, for geostatistical modeling, prediction, and simulation, rgdal, rgeos and maptools. Chapter 3 is devoted to the visualization of spatial data. Code, figures, and data of the book are available at the accompanying website7.
  • (Hengl 2009) is an open-access book with seven spatial data analysis exercises. The author is the creator and maintainer of the Spatial-Analyst webpage8.
  • The CRAN Tasks View “Analysis of Spatial Data”9 summarizes the packages for reading, vizualizing, and analyzing spatial data. The packages in development published at R-Forge are listed in the “Spatial Data & Statistics” topic view10. The R-SIG-Geo mailing list11 is a powerful resource for obtaining help.
  • The “Spatial Analysis”12 and “Kartograph”13 webpages publish a variety of beautiful visualization examples.

1 Although sp and raster are the most important packages, there are an increasing number of packages designed to work with spatial data. They are summarized in the corresponding CRAN Task View. Read Section 7.2 for details.

2 http://CRAN.R-project.org/view=Spatial

3 http://trac.osgeo.org/geos/

4 http://www.gdal.org/

5 https://trac.osgeo.org/proj/

6 http://www.pearsonhighered.com/slocum3e/ and http://highered.mcgraw-hill.com/sites/0072943823/

7 http://www.asdar-book.org/

8 http://spatial-analyst.net

9 http://CRAN.R-project.org/view=Spatial

10 http://r-forge.r-project.org/softwaremap/trove_list.php?form_cat=353

11 https://stat.ethz.ch/mailman/listinfo/R-SIG-Geo/

12 http://spatialanalysis.co.uk/map-gallery/

13 http://kartograph.org/

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.206.254