Last data update: 2014.03.03

R: Create a Grid and Clip it to a Map and Data Bounds
predgridR Documentation

Create a Grid and Clip it to a Map and Data Bounds

Description

Creates a data frame containing a rectangular grid of points to cover the range of X and Y coordinates provided in a data set, and trims the grid so that the points do not extend beyond the boundaries shown on a map.

Usage

predgrid(dataXY, map = NULL, nrow = 100, ncol = 100)

Arguments

dataXY

A data frame with X and Y coordinates (required). If the data frame has more than 2 columns, the X and Y coordinates must be in the 2nd and 3rd columns, respectively (the format required for the modgam function). If the data frame has only 2 columns, the X coordinates must be in the 1st column and Y coordinates in the 2nd column.

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 maptools package. If map = NULL (default), a rectangular grid is produced covering the range of geolocations within dataXY.

nrow

number of rows in the grid (default=100)

ncol

number of columns in the grid (default=100)

Details

predgrid creates a grid of dimensions nrow*ncol using the range of X and Y coordinates (e.g., longitude and latitude) in the data frame supplied as dataXY. If the map argument is used, the function trimdata is used to clip the grid. Users should be sure to use the same projection for the map and the data; putting both on the same plot can help reveal differing projections. 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.

Value

A data frame with X and Y coordinates in the first two columns. The column names from dataXY are used for the output. If the columns of dataXY are unnamed then the names "X" and "Y" are assigned to the data frame.

Author(s)

Veronica Vieira, Scott Bartell, and Robin Bliss

Send bug reports to sbartell@uci.edu.

See Also

trimdata, optspan, modgam, colormap.

Examples

# define a rectangular 100x100 grid covering the MA data 
data(MAdata)
gamgrid <- predgrid(MAdata)
# plot the grid points
plot(gamgrid$Xcoord, gamgrid$Ycoord, cex=0.1, col="red")
# and the data locations
points(MAdata$Xcoord,MAdata$Ycoord)

# But that grid extends beyond the state boundaries and into the ocean!
# Better to also clip the grid to a map of MA using the following code
# which requires the PBSmapping package:
if (require(PBSmapping)) {
# Clip a 50x50 grid covering the MA data to a map of MA 
data(MAmap)
gamgrid2 <- predgrid(MAdata, map=MAmap, nrow=50, ncol=50)
# plot the MA map and grid points
plot(MAmap)
points(gamgrid2$Xcoord, gamgrid2$Ycoord, cex=0.1, col="red")
}

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(MapGAM)
Loading required package: sp
Loading required package: gam
Loading required package: splines
Loading required package: foreach
Loaded gam 1.12

> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/MapGAM/predgrid.Rd_%03d_medium.png", width=480, height=480)
> ### Name: predgrid
> ### Title: Create a Grid and Clip it to a Map and Data Bounds
> ### Aliases: predgrid
> ### Keywords: misc
> 
> ### ** Examples
> 
> # define a rectangular 100x100 grid covering the MA data 
> data(MAdata)
> gamgrid <- predgrid(MAdata)
> # plot the grid points
> plot(gamgrid$Xcoord, gamgrid$Ycoord, cex=0.1, col="red")
> # and the data locations
> points(MAdata$Xcoord,MAdata$Ycoord)
> 
> # But that grid extends beyond the state boundaries and into the ocean!
> # Better to also clip the grid to a map of MA using the following code
> # which requires the PBSmapping package:
> if (require(PBSmapping)) {
+ # Clip a 50x50 grid covering the MA data to a map of MA 
+ data(MAmap)
+ gamgrid2 <- predgrid(MAdata, map=MAmap, nrow=50, ncol=50)
+ # plot the MA map and grid points
+ plot(MAmap)
+ points(gamgrid2$Xcoord, gamgrid2$Ycoord, cex=0.1, col="red")
+ }
Loading required package: PBSmapping

-----------------------------------------------------------
PBS Mapping 2.69.76 -- Copyright (C) 2003-2016 Fisheries and Oceans Canada

PBS Mapping comes with ABSOLUTELY NO WARRANTY;
for details see the file COPYING.
This is free software, and you are welcome to redistribute
it under certain conditions, as outlined in the above file.

A complete user guide 'PBSmapping-UG.pdf' is located at 
/home/ddbj/local/lib64/R/library/PBSmapping/doc/PBSmapping-UG.pdf

Packaged on 2015-04-23
Pacific Biological Station, Nanaimo

All available PBS packages can be found at
http://code.google.com/p/pbs-software/

To see demos, type '.PBSfigs()'.
-----------------------------------------------------------


> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>