Plotting maps

From SourceWiki
Revision as of 18:12, 3 August 2008 by Wbuytaert (talk | contribs)
Jump to navigation Jump to search

Introduction

R can plot very nice maps. I decided to give up plotting maps in grass. The ps.map function is very powerful, but hard to remember for my not-so-frequent use. I use the grass interface but R imports very many raster and vector formats via the rgdal package.

Plotting GIS data is very nicely implemented in the sp package, part of the spatial data in R project, but getting your plot the way you want is quite challenging. Here is an example.

Required packages

spgrass6 reads data from the GRASS database, use rgdal for other formats. sp implements spplot, and lattice is used to define the colours (trells.par.set).

  library(sp)
  library(spgrass6)
  library(lattice)

Plotting a map

  #direct import from GRASS, use readGDAL() for other formats
  
  paute <- rast.get6("paute_DEM")
  
  # set some nice topographic colours, if you want dull greyscale colors, use:
  # colours <- gray(c(rep(100,10),100:15)/100)
  
  colours <- topo.colors(1000)
  trellis.par.set(sp.theme(regions=list(col = colours)))
  
  # define the objects necessary to draw a scalebar and north arrow:
  
  scale = list("SpatialPolygonsRescale", layout.scale.bar(height=0.1), offset = c(690000,9735000), scale = 20000, fill=c("transparent","black"))
  text1 = list("sp.text", c(690000,9740000), "0")
  text2 = list("sp.text", c(710000,9740000), "20 km")
  arrow = list("SpatialPolygonsRescale", layout.north.arrow(type=1), offset = c(698000,9720000), scale = 10000)
  
  # and finally plot the map:
  
  spplot(paute_DEM,draw = T, colorkey=list(space="right", height=0.5), cuts=127,
     scales=list(draw=T), sp.layout=list(scale,text1,text2,arrow))

Good to know:

  • cuts defines the number of colour intervals. The default is 15 and that's not really nice for continuous DEM images
  • the colorkey options are part of levelplot {lattice} and are quite customizable

Results

Map paute.png