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))

1 comment :

  1. Congratulations, one of the best site about R

    Good example

    Thankyou for your dedication

    ReplyDelete