Book Image

R Graphs Cookbook Second Edition

Book Image

R Graphs Cookbook Second Edition

Overview of this book

Table of Contents (22 chapters)
R Graphs Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Working with ESRI shapefiles


In this recipe, we will learn how to read and write geographic data in the form of shapefiles (.shp), using Geographical Information Systems (GIS) software created by ESRI and some other similar software.

Getting ready

We are going to use the maptools package for this recipe. So, let's install and load it first:

install.packages("maptools")
library(maptools)

How to do it...

We are going to read an example shapefile provided with the maptools package and plot it:

sfdata <- readShapeSpatial(system.file("shapes/sids.shp", 
package="maptools")[1], proj4string=CRS("+proj=longlat"))

plot(sfdata, col="orange", border="white", axes=TRUE)

To write out the data as another shapefile, we can do:

writeSpatialShape(sfdata,"xxpoly")

How it works...

We used the readShapeSpatial() function of the maptools package to read in a shapefile. This function takes a shapefile name as an argument and reads the data into a SpatialPolygonsDataFrame object. The first argument in the example is...