Learn R Programming

CCAMLRGIS (version 3.0.4)

create_PolyGrids: Create a Polygon Grid

Description

Create a polygon grid to spatially aggregate data in cells of chosen size. Cell size may be specified in degrees or as a desired area in square kilometers (in which case cells are of equal area).

Usage

create_PolyGrids(
  Input,
  OutputFormat = "ROBJECT",
  OutputName = NULL,
  dlon = NA,
  dlat = NA,
  Area = NA,
  cuts = 100,
  cols = c("green", "yellow", "red")
)

Arguments

Input

the name of the Input data as a .csv file or an R dataframe. If a .csv file is used as input, this file must be in your working directory and its name given in quotes e.g. "DataFile.csv".

The columns in the Input must be in the following order:

Latitude, Longitude, Variable 1, Variable 2 ... Variable x.

OutputFormat

can be an R object or an ESRI Shapefile. if OutputFormat is specified as "ROBJECT" (the default), a SpatialPolygonDataFrame is created in your R environment. if OutputFormat is specified as "SHAPEFILE", an ESRI Shapefile is exported in your working directory.

OutputName

if OutputFormat is specified as "SHAPEFILE", the name of the output shapefile in quotes (e.g. "MyGrid") must be provided.

dlon

width of the grid cells in decimal degrees of longitude.

dlat

height of the grid cells in decimal degrees of latitude.

Area

area, in square kilometers, of the grid cells.

cuts

Number of desired color classes.

cols

Desired colors. If more that one color is provided, a linear color gradient is generated.

Value

Spatial object in your environment or ESRI shapefile in your working directory. Data within the resulting spatial object contains the data provided in the Input after aggregation within cells. For each Variable, the minimum, maximum, mean, sum, count, standard deviation, and, median of values in each cell is returned.

To see the data contained in your spatial object, type: View(MyGrid@data).

In addition, colors are generated for each aggregated values according to the chosen cuts (numerical classes) and cols (colors). To generate a custom color scale, refer to add_col and add_Cscale.

See Also

create_Points, create_Lines, create_Polys, create_Stations, add_RefGrid, add_col, add_Cscale.

Examples

Run this code
# NOT RUN {

#Example 1: Simple grid, using automatic colors

MyGrid=create_PolyGrids(GridData,dlon=2,dlat=1)
#View(MyGrid@data)
plot(MyGrid,col=MyGrid$Col_Catch_sum)

#Example 2: Equal area grid, using automatic colors

MyGrid=create_PolyGrids(GridData,Area=10000)
plot(MyGrid,col=MyGrid$Col_Catch_sum)

#Example 3: Equal area grid, using custom cuts and colors

#MyGrid=create_PolyGrids(GridData,Area=10000,cuts=c(0,50,100,500,2000,3500),cols=c('blue','red'))
plot(MyGrid,col=MyGrid$Col_Catch_sum)

#Example 4: Equal area grid, using custom cuts and colors, and adding a color scale (add_Cscale)

#Step 1: Generate your grid
MyGrid=create_PolyGrids(GridData,Area=10000)

#Step 2: Inspect your gridded data (e.g. sum of Catch) to
#determine whether irregular cuts are required
hist(MyGrid$Catch_sum,100) 
#In this case (heterogeneously distributed data) irregular cuts would be preferable

#Step 3: Generate colors according to the desired classes (cuts)
Gridcol=add_col(MyGrid$Catch_sum,cuts=c(0,50,100,500,2000,3500),cols=c('yellow','purple'))

#Step 4: Plot result and add color scale
Mypar=par(mai=c(0,0,0,2)) #Figure margins as c(bottom, left, top, right)
plot(MyGrid,col=Gridcol$varcol) #Use the colors generated by add_col
#Add color scale using cuts and cols generated by add_col
add_Cscale(title='Sum of Catch (t)',cuts=Gridcol$cuts,cols=Gridcol$cols,width=24) 
par(Mypar)

# }
# NOT RUN {
# }

Run the code above in your browser using DataLab