Visualizing sp-classes

This page discusses topics related to spatial data visualization in R.

When running the example code it is assumed that the sp package is loaded. When referring to meuse or meuse.grid it is assumed that the datasets are loaded and converted to spatial objects:

coordinates(meuse) = ~x+y
gridded(meuse.grid) = ~x+y

A gallery of some spatial plots of sp-classes can be found here. It includes the R code that created the plots.


The workhorse of spatial data visualization is the spplot() command from the sp package. An example for point sets:

spplot(meuse, "zinc")

and for a grid:

spplot(meuse.grid, "dist")

Changing the color scale

When changing a color scale two spplot parameters are important, at and col.regions. at specifies where the color breaks are and col.regions lists the colors for each break. An example in code:

at = seq(100,1900,200)
col = rainbow(length(at))
# For point plots the cuts = argument gives the number of cuts to use
# make it equal to the amount of breaks
spplot(meuse, "zinc", at = at, col.regions = col, cuts = length(at))
at = seq(0,1, 0.1)
spplot(meuse.grid, "dist", at = at, col.regions = col)

The R package classInt provides a function to determine useful breaks based on the data using different styles:

# Use inverse distance weighted interpolation to make a zinc grid
meuse.grid$zinc = idw(zinc~1, meuse, meuse.grid)$var1.pred
at = classIntervals(meuse.grid$zinc, n = 10, style = "equal")$brks
spplot(meuse.grid, "zinc", at = at)
at = classIntervals(meuse.grid$zinc, n = 10, style = "fisher")$brks
spplot(meuse.grid, "zinc", at = at)

The R package RColorBrewer provides color ramps for thematic maps:

# Display all possible palettes:
col.regions = brewer.pal(3, "RdYlGn")
# Convert the meuse.grid$soil to a factor
meuse.grid$soil = as.factor(meuse.grid$soil)
spplot(meuse.grid, "soil", col.regions = col.regions)

Adding GIS data as layout

If you make a plot of some variable you often want to add some additional GIS layers such as the locations of measurements or a polygon of the study area. In sp the most straightforward way is through the sp.layout argument. The sp.layout is a list containing a list for every GIS layer you want to add. The first argument states what kind of data the GIS layer is: ‘sp.points’ for SpatialPoints, ‘sp.polygons’ for SpatialPolygons object, ‘sp.lines’ for a SpatialLines object, and ‘sp.text’ for text to place. After that we put the sp object containing the GIS layer we would like to add. Finally we can add arguments to tweak how the GIS layer is drawn, for example pch to choose the plotting character. An example in R-code:

l1 = list("sp.points", meuse, pch = 19)
spplot(meuse.grid, "dist", sp.layout = list(l1))

See ?spplot for some more examples, including how to draw a north arrow and a scale bar.

A more flexible way to add layout GIS layers is using a custom panel function. This comes in handy if for example you want to draw your polygon on top of the grid you plot (default is other way around). The R-code to solves this looks like (for this example we also add an additional point layer):

spplot(grid_object, "layer_to_plot",
    panel = function(...) {

Get rid of the key / legend

Sometimes you don’t want spplot drawing a key (or legend). For SpatialPoint* you can abuse the argument:

spplot(meuse, "zinc", = "")

Normally is used to position the key. For SpatialGrid* and SpatialPolygon* objects you can use the colorkey argument:

spplot(meuse.grid, "dist", colorkey = FALSE)
tips/spatial-data/spatial_data_visualization.txt · Last modified: 2009/09/23
Recent changes RSS feed R Wiki powered by Driven by DokuWiki and optimized for Firefox Creative Commons License