##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