Difference between revisions of "Plotting maps"

From SourceWiki
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
I decided to give up on plotting nice maps with [http://grass.itc.it grass]. The ps.map function is very powerful, but hard to remember for my not-so-frequent use. R has great plotting capabilities, and of course it does maps. It has an easy [http://cran.r-project.org/src/contrib/Descriptions/spgrass6.html grass interface]. What else do you want (importing other GIS formats? Try [http://cran.r-project.org/src/contrib/Descriptions/rgdal.html rgdal]). Plotting GIS data is very nicely implemented in the [http://cran.r-project.org/src/contrib/Descriptions/sp.html sp] package, part of the [http://r-spatial.sourceforge.net/ spatial data in R] project, but getting your plot the way you want is quite challenging. Here's what I came up with:
+
[[Category:Projects]]
 +
[[Category:Hydrology in R]]
 +
==Introduction==
  
      library(spgrass6)
+
R can plot very nice maps. I decided to give up plotting maps in [http://grass.itc.it grass]. The ps.map function is very powerful, but hard to remember for my not-so-frequent use. I use the [http://cran.r-project.org/src/contrib/Descriptions/spgrass6.html grass interface] but R imports very many [http://www.gdal.org/formats_list.html raster] and [http://www.gdal.org/ogr/ogr_formats.html vector] formats via the [http://cran.r-project.org/src/contrib/Descriptions/rgdal.html rgdal] package.
      library(lattice)
+
 
      paute <- rast.get6("paute_z")
+
Plotting GIS data is very nicely implemented in the [http://cran.r-project.org/src/contrib/Descriptions/sp.html sp] package, part of the [http://r-spatial.sourceforge.net/ spatial data in R] project, but getting your plot the way you want is quite challenging. Here is an example.
      colours <- topo.colors(1000) # if you want pretty topographic colours
+
 
      colours <- gray(c(rep(100,10),100:15)/100) # for greyscale colours
+
==Required packages==
      trellis.par.set(sp.theme(regions=list(col = colours)))
+
 
      scale = list("SpatialPolygonsRescale", layout.scale.bar(height=0.1), offset = c(690000,9735000), scale = 20000, fill=c("transparent","black"))
+
spgrass6 reads data from the GRASS database, use rgdal for other formats. sp implements spplot, and lattice is used to define the colours (trellis.par.set).
      text1 = list("sp.text", c(690000,9740000), "0")
+
 
      text2 = list("sp.text", c(710000,9740000), "20 km")
+
  library(maptools)
      arrow = list("SpatialPolygonsRescale", layout.north.arrow(type=1), offset = c(698000,9720000), scale = 10000)
+
  library(spgrass6)
      spplot(paute,draw = T, colorkey=list(space="right", height=0.5), cuts=127,
+
  library(lattice)
              scales=list(draw=T), sp.layout=list(scale,text1,text2,arrow))
+
 
 +
==Plotting a map==
 +
 
 +
  # read the DEM and the vectors (for importing other formats see the rgdal package)
 +
 
 +
  paute_DEM <- readAsciiGrid("paute_DEM.arc", proj4string = CRS("+proj=tmerc +datum=WGS84"))
 +
  paute_rivers <- readShapeLines("paute_rivers", proj4string = CRS("+proj=tmerc +datum=WGS84"))
 +
  paute_borders <- readShapeLines("paute_borders", proj4string = CRS("+proj=tmerc +datum=WGS84"))
 +
 
 +
  # set some nice topographic colours, if you want dull greyscale colors, use:
 +
  # colours <- gray(c(rep(100,10),100:15)/100)
 +
 
 +
  colours <- terrain.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.05), offset = c(-79.3,-2.45), scale = 0.18, fill=c("transparent","black"))
 +
  text1 = list("sp.text", c(-79.28,-2.45), "0")
 +
  text2 = list("sp.text", c(-79.1,-2.45), "20 km")
 +
  arrow = list("SpatialPolygonsRescale", layout.north.arrow(type=1), offset = c(-79.2,-2.35), scale = 0.1)
 +
 
 +
  # define the vectors:
 +
 
 +
  rivers  <- list("sp.lines",paute_rivers, col="blue", lwd=0.5)
 +
  limits  <- list("sp.lines",paute_limits, col="black", lwd=1)
 +
 
 +
  # 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,rivers,limits))
  
 
Good to know:
 
Good to know:
Line 18: Line 50:
 
* the ''colorkey'' options are part of levelplot {lattice} and are quite customizable
 
* the ''colorkey'' options are part of levelplot {lattice} and are quite customizable
  
The result looks like this:
+
==Results==
  
 
[[Image:map_paute.png]]
 
[[Image:map_paute.png]]
 
 
--[[User:Wbuytaert|Wbuytaert]] 17:53, 21 January 2008 (UTC)
 

Latest revision as of 08:57, 7 August 2008

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 (trellis.par.set).

  library(maptools)
  library(spgrass6)
  library(lattice)

Plotting a map

 # read the DEM and the vectors (for importing other formats see the rgdal package)
 
 paute_DEM <- readAsciiGrid("paute_DEM.arc", proj4string = CRS("+proj=tmerc +datum=WGS84"))
 paute_rivers <- readShapeLines("paute_rivers", proj4string = CRS("+proj=tmerc +datum=WGS84"))
 paute_borders <- readShapeLines("paute_borders", proj4string = CRS("+proj=tmerc +datum=WGS84"))
 
 # set some nice topographic colours, if you want dull greyscale colors, use:
 # colours <- gray(c(rep(100,10),100:15)/100)
 
 colours <- terrain.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.05), offset = c(-79.3,-2.45), scale = 0.18, fill=c("transparent","black"))
 text1 = list("sp.text", c(-79.28,-2.45), "0")
 text2 = list("sp.text", c(-79.1,-2.45), "20 km")
 arrow = list("SpatialPolygonsRescale", layout.north.arrow(type=1), offset = c(-79.2,-2.35), scale = 0.1)
 
 # define the vectors:
 
 rivers  <- list("sp.lines",paute_rivers, col="blue", lwd=0.5)
 limits  <- list("sp.lines",paute_limits, col="black", lwd=1)
 
 # 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,rivers,limits))

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