Learn R Programming

McSpatial (version 1.1.1)

mapoverlay: Merges variables across shape and point files

Description

Associates values for a shape file variable with the observations in a point file. Also can match means, sums, or other functions of point file observations to a shape file.

Usage

mapoverlay(shpfile,pointfile,shpvar,pointvar=NULL,func="sum")

Arguments

shpfile
A shape file.
pointfile
A data frame with point data. Must include the geographic coordinates for the point data. The geographic coordinates must be stored in variables named longitude and latitude.
shpvar
The name of the variable in the shape file whose values are to be added to the point data. Must be in quotes. Example: shpvar="TRACT" or shpvar="city".
pointvar
Either NULL or the name of the variable in pointfile. In the latter case, the name must be in quotes, e.g., pointvar="price".
func
The function to be applied to pointvar, such as "mean" or "sd". If pointvar=NULL, func is ignored. Default: func = "sum".

Value

  • pointoutA variable with the values of shpvar, matched to the observations in pointfile. Stored in the same order as the pointfile observations, so it can be added directly to pointfile.
  • shpoutA variable with the values of pointvar, matched to the observations in shpfile. Stored in the same order as the observations in shpfile.

Details

The mapoverlay command serves as a simple interface to the sp package's overlay command. As an example, shpfile might represent census tracts while pointfile includes data on home sales, including the geographic coordinates. The geographic coordinates for pointfile must be named longitude and latitude, shpfile must be a polygon file, and pointfile must be a data frame. In this example, suppose that the name of the census tract variable in shpfile is "TRACT". Also, assume there are n1 observation in shpfile and n2 observations in pointfile. Then the output from fit <- mapoverlay(shpfile,pointfile,shpvar="TRACT") is a vector with n2 entries indicating the tract in which each pointfile observation is located. The variable can be added to pointfile data set using the command pointfile$TRACT <- fit$pointout. While the shpvar option is required, pointvar is only needed to aggregate a pointfile variable to the shape file. For example, suppose we want to calculate the average home sale price in each census tract. The command fit <- mapoverlay(shpfile, pointfile, shpvar="TRACT", pointvar="price", func="mean") calculates the mean of pointfile$price for each value of shpfile$TRACT. In this case, fit$pointout is the same as before, but fit$shpout is a variable with n1 entries with the average values of pointfile$price. It can be added directly to the shape file with the command shpfile$price <- fit$shpout.

See Also

mapplot mappoints

Examples

Run this code
library(maptools)
cmap <- readShapePoly(system.file("maps/CookCensusTracts.shp",
  package="McSpatial"))
data(matchdata)
matchdata$price <- exp(matchdata$lnprice)
fit <- mapoverlay(cmap,matchdata,shpvar="TRACT",
  pointvar="price",func="mean")
matchdata$TRACT <- fit$pointout
table(matchdata$TRACT)
cmap$avgprice <- fit$shpout
psamp <- !is.na(cmap$avgprice)
mapplot(cmap,"avgprice",sampvar=psamp)

Run the code above in your browser using DataLab