How to use R-package ‘maptools’ to read and display shapefiles

Maptools

This tutorial contains an example on how to read, access and plot ESRI shapefiles in R by using the R-package maptools.

Reference manual:  http://cran.r-project.org/web/packages/maptools/maptools.pdf
Set of tools for manipulating and reading geographic data, in particular ESRI shapefiles. Depends on library ‘sp’

Example:
For further information about the data which is used in this example please visit: http://www.naturalearthdata.com/downloads/10m-cultural-vectors/10m-admin-0-countries/

# Install Packages
install.packages('maptools')

# Loads 'maptools' library in R
library(maptools)

# Download Shapefile
url.c='http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/ne_10m_admin_0_countries.zip'
dfile='ne_10m_admin_0_countries.zip'
download.file(url.c, destfile=dfile, method='wget')

# Unzip Shapefile
unzip('ne_10m_admin_0_countries.zip', exdir='countries')
#Remove .zip file
file.remove('ne_10m_admin_0_countries.zip')

# Read Shapefile of Countries
cshp=readShapePoly('countries/ne_10m_admin_0_countries')

# Plot all Polygons in rainbow-color
plot(cshp,col=rainbow(nrow(cshp)),border='grey')
title('Countries of the World')
mtext('Source:\n http://www.naturalearthdata.com/downloads/10m-cultural-vectors/10m-admin-0-countries/',side=1)

# Add Australia in grey color
au=which(cshp@data$NAME=='Australia')
plot(cshp[au,],col='grey',border='black',add=T)

# Coordinates of Polygon Australia (au)
cshp@polygons[[au]]@Polygons[[1]]@coords

# Min and Max of Lat and Lon
cshp[au,]@bbox

maptools