[R-sig-Geo] Plotting a shapefile

Roger Bivand Roger.Bivand at nhh.no
Mon Oct 16 21:14:32 CEST 2006


On Mon, 16 Oct 2006 matt.pettis at thomson.com wrote:

> Hi,
>  
> I am trying to read in a shapefile, which is a state map broken into
> voting precints, and plot a map of the result.  There is no measure data
> as of yet -- only the shapefile with boundaries.  Can anybody give me
> direction on how to start this very basic task?

There are two ways to read the shapefile into a SpatialPolygonsDataFrame:

library(maptools)
getinfo.shape("shpfile.shp")
# to check that it is a polygon file
my_polys <- readShapePoly("shpfile.shp")
# this has two arguments, IDvar=, which takes the name (as a string) of a 
# field in the DBF file to use for IDs, and proj4string= for inserting the 
# coordinate reference system

or

library(rgdal)
# assuming you can install the rgdal package, easy on Windows, from source 
# with external dependencies elsewhere
my_polys <- readOGR(".", "shpfile")
# with no way of setting the IDs to a DBF field (they are the OGR internal 
# IDs, but reads the *.prj file automatically; the first argument is the 
# name of the directory (here the current directory), the second the file 
# name without extension

>From there, it's just:

plot(my_polys)

The plot method can be configured using arguments, including filling the 
polygons with colours or hatching, colouring the boundaries and setting 
the line thicknesses etc., and plotting a smaller window using xlim= and 
ylim= as usual in plot methods. 

The arguments are:

> args(sp:::plot.SpatialPolygons)
function (x, col, border = par("fg"), add = FALSE, xlim = NULL, 
    ylim = NULL, xpd = NULL, density = NULL, angle = 45, pbg = NULL, 
    axes = FALSE, ...) 

and in fact (this will be dealt with in the next sp release) are only 
documented under:

?plot.polylist 

in the maptools package, with the exception of pbg=, which is the 
background colour also used to fill holes provided that the holes (aka. 
lakes) are correctly given.

Roger

>  
> thanks,
> Matt
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list