[R] Graphically show population density for a specific geographicarea using R

Roger Bivand Roger.Bivand at nhh.no
Mon May 21 22:18:24 CEST 2007

On Mon, 21 May 2007, Greg Snow wrote:

> 1. Yes you need a shape file with that information.
> 2. One place to look is:
> http://www.census.gov/geo/www/cob/bdy_files.html There are shapefiles
> (the .shp ones) for counties and zip codes (as well as many other
> things).  These work pretty directly with the tools in the maptools and
> sp packages.
> 3. Read the .shp file in using maptools, the .dbf information is
> automatically read in as well (with whatever details come with the
> shapefile), then look at the structure of what you read in, you will see
> the data frame and you should be able to just add your data into that
> data frame, or use it seperately.

wget http://www.census.gov/geo/cob/bdy/zt/z500shp/zt17_d00_shp.zip
unzip zt17_d00_shp.zip

ill_zip <- readShapePoly("zt17_d00.shp", proj4string=CRS("+proj=longlat"))
plot(ill_zip, axes=TRUE)

suggests that there is no useful attribute data in the shapefile. It is in 
geographical coordinates, so that the AREA variable is not in useful 
coordinates. If you look at the codes, you'll see that some are 
represented by multiple polygons, especially the HH codes (water 
features). Also note that ZCTA are not the same as Postal ZIP Codes, 
according to the FAQ.

The easiest way to map a variable at the moment is:

spplot(ill_zip, "AREA")
> ill_zip$HH <- FALSE
> ill_zip$HH[grep("HH", as.character(ill_zip$NAME))] <- TRUE
> spplot(ill_zip, "HH")

only AREA doesn't tell you anything. You'll need to look for data for
ZCTAs or similar, and if you need to compute the areas, project to
something suitable and extract the values from the underlying
SpatialPolygons object.

Finally, you'll need up-to-date numbers of inhabitants, as at this scale, 
a housing development can easily date your data.

I suggest you follow up on R-sig-geo.


> 4. plot.Map has an option auxvar, though it is now prefered to use the
> plot methods for the more specific objects (SpatialLinesDataFrame object
> from the readLinesShape function).  I would expect that they would have
> an auxvar or similar argument also.  Or you can decide what color you
> want to use, plot the overall map, then add each county/zipcode
> individually using the specified color.
> Hope this helps,

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-help mailing list