24 Oct 2011

A Simple Example for the Use of Shapefiles in R

A simple example for drawing an occurrence-map (polygons with species' points) with the R-packages maptools and sp using shapefiles.
HERE is the example data.








library(maptools)
library(sp)

# Note that for each shapefile, you only need to read the .shp component
# the others will be read in at the same time automatically.
# TIRIS.BEZIDX_PL.shp contains the political districts of North-Tyrol
# Limodorum.shp contains points with occurences of the plant species
# Limodorum abortivum.
# Note that my layers use the same geographic coordinate systems (gcs),
# using other data you would need to check if the gcs and
# projection of all layers are the same!  

# set dir to where you downloaded the data:
setwd("E:/R/Data/Maps/Example.1_Data")
Limodorum.shp <- readShapePoints(file.choose())
TIRIS.BEZIDX_PL.shp <- readShapePoly(file.choose())

# examine points:
summary(Limodorum.shp)
attributes(Limodorum.shp@data)

# examine polygons:
summary(TIRIS.BEZIDX_PL.shp)
attributes(TIRIS.BEZIDX_PL.shp@data)

# limits:
# to customize ylim in plot-call
# seems not to work here...
xlim <- TIRIS.BEZIDX_PL.shp@bbox[1, ]
ylim <- TIRIS.BEZIDX_PL.shp@bbox[2, ]      
                                           
par(mai = rep(.1, 4))

plot(TIRIS.BEZIDX_PL.shp, col = "grey93", axes = F,
     xlim = xlim, ylim = ylim, bty = "n")
points(Limodorum.shp, pch = 16,
       col = 2, cex = .5)
mtext("Limodorum abortivum", 3, line = -7,
      at = -17000, adj = 0, cex = 2, font = 3)
legend("bottomleft", inset = c(0.4, 0.2),
       legend = c("Fundpunkte", "Polit. Bezirke"),
       bty = "n", pch = c(16,-1),           # bty = "n": no box
       col = c(2, 1), pt.cex = c(.5, 1),
       lty = c(-1, 1))


To cite package ‘maptools’ in publications use:

  Nicholas J. Lewin-Koh, Roger Bivand, contributions by Edzer J.
  Pebesma, Eric Archer, Adrian Baddeley, Hans-Jörg Bibiko, Stéphane
  Dray, David Forrest, Michael Friendly, Patrick Giraudoux, Duncan
  Golicher, Virgilio Gómez Rubio, Patrick Hausmann, Karl Ove
  Hufthammer, Thomas Jagger, Sebastian P. Luque, Don MacQueen, Andrew
  Niccolai, Tom Short, Ben Stabler and Rolf Turner (2011). maptools:
  Tools for reading and handling spatial objects. R package version
  0.8-10. http://CRAN.R-project.org/package=maptools


To cite package ‘sp’ in publications use:

  Pebesma, E.J., R.S. Bivand, 2005. Classes and methods for spatial
  data in R. R News 5 (2), http://cran.r-project.org/doc/Rnews/.

  Roger S. Bivand, Edzer J. Pebesma, Virgilio Gomez-Rubio, 2008.
  Applied spatial data analysis with R. Springer, NY.
  http://www.asdar-book.org/

No comments :

Post a Comment