Learn R Programming

spBayes (version 0.2-4)

hexGrid: Generates a hexagon tessellation within a bounding box

Description

This function generates a hexagon tessellation within a bounding box.

Usage

hexGrid(nodes.per.layer, b.box, ...)

Arguments

nodes.per.layer
the number of hexagons from min.x to max.x at min.y.
b.box
the domain's bounding box defined with a vector of length four and elements ordered min.x, min.y, max.x, max.y.
...
currently no additional arguments.

Value

  • A list with the following tags:
  • hx.hya vector of length two that holds the height and width, respectively, between hexagon centroids.
  • layersthe number of hexagon layers within the domain.
  • nodes.per.layerthe number of hexagons from min.x to max.x at min.y.
  • hex.centroidsan $n\times 2$ matrix with rows corresponding the the $x$ and $y$ coordinates of the $n$ hexagons within the domain.
  • hex.polygonsa list with $6\times 2$ matrices which hold the hexagons' $x$ and $y$ vertices coordinates.

Examples

Run this code
##Define the bounding box and make the call
b.box <- c(0,0,10,10)

out <- hexGrid(20, b.box)

##Plot using lapply
par(mfrow=c(1,2))
    
plot(out$hex.centroids, pch=19, cex=0.5,
     ylab="Northing", xlab="Easting")
lapply(out$hex.polygons, polygon, col="blue")

##Now color hexagons based on value
my.col.ramp <- function(z){
  zlim <- range(z)
  zlen <- zlim[2]-zlim[1]+1
  colorlut <- heat.colors(as.integer(zlen))
  col <- colorlut[z-zlim[1]+1]
  col
}

n <- nrow(out$hex.centroids)
col <- my.col.ramp(rnorm(n))

plot(out$hex.centroids, typ="n", ylab="", xlab="Easting")
for(i in 1:n){
  polygon(out$hex.polygons[[i]], col=col[i], border=col[i])
}

Run the code above in your browser using DataLab