Learn R Programming

MapGAM (version 0.6-2)

trimdata: Trim a Data Set To Map Boundaries

Description

Takes a subset of a data frame: returns rows with geolocations inside the boundaries determined by a map. If rectangle=FALSE, strict map boundaries are used. If rectangle=TRUE, a rectangular boundary is determined based on the range of X and Y coordinates for the map, along with an optional buffer.

Usage

trimdata(rdata, map, Xcol=2, Ycol=3, rectangle=F, buffer=0.05)

Arguments

rdata
Data set (required). If rdata has only 2 columns then they are assumed to be the X and Y coordinates, in that order. If rdata has more than 2 columns then identify the positions of the X and Y coordinates with Xcol
map
A map for clipping the grid, provided in a recognized format. Supported classes include "map", the format produced by the maps package, and "SpatialPolygonsDataFrame", which can be produced from an ESRI shapefile using the readShapePoly function in the m
Xcol
The column number of rdata for the X coordinates of geolocation. Only used if rdata has >2 columns.
Ycol
The column number of rdata for the Y coordinates of geolocation. Only used if rdata has >2 columns.
rectangle
If rectangle=FALSE (default), only data with geolocations strictly within the map boundaries are retained. If rectangle=TRUE, only data with geolocations within a rectangular boundary are retained. The rectangular boundary coor
buffer
A fraction of the map range to add to either side of the rectangular boundary before trimming the data (default 5%). The buffer argument is ignored if rectangle=FALSE. If a vector of length 2 is supplied, the first value as the buffer fract

Value

  • A subset of the rdata data frame containing only those rows with geolocations within the specified boundaries.

Details

Various functions from the PBSmapping package and maptools package are used to convert map formats, check whether the map and data are aligned, and clip the grid. Be sure to use fill=T when using the map function to produce maps for trimdata: maps produced using the default fill=F argument do not work properly with this function. If the map centroid is not in the range of the data a warning message is printed; this might indicate differing projections but can occur naturally when the data were not sampled from the entire extent of the map or when map boundaries are concave.

See Also

predgrid, optspan, modgam, colormap.

Examples

Run this code
# These examples require the "PBSmapping" and "maps" packages
if (require(maps) & require(PBSmapping)) {
data(beertweets)
dim(beertweets)

### Trim data to US base map, and plot them
basemap1 <- map("usa", fill=TRUE, col="transparent")	
dUS <- trimdata(beertweets, basemap1)	 				 
# Plot tweet locations (beer tweets in red)
points(dUS$longitude, dUS$latitude, col=dUS$beer+1, cex=0.5)		

### Trim data to Texas base map, and plot them
basemap2 <- map("state", regions="texas", fill=TRUE, col="transparent") 
dTX <- trimdata(beertweets, basemap2) 								
# Plot tweet locations (beer tweets in red)
points(dTX$longitude, dTX$latitude, col=dTX$beer+1, cex=0.5) 
}

Run the code above in your browser using DataLab