matrixPoly: Make polygons from a matrix

View source: R/matrixPoly.R

matrixPolyR Documentation

Make polygons from a matrix

Description

matrixPoly creates a list of polygon coordinates given a matrix z and corresponding x and y coordinates for the dimensions of z.

Usage

matrixPoly(x, y, z, n = NULL)

Arguments

x, y

Optional vectors of values for matrix z rows (x) and columns (y). If excluded, the function assumes the values to be a evenly-spaced sequence from 0 to 1.

z

A matrix

n

An optional vector of element positions of z for polygon creation

Value

List of polygon coordinates for each element of z

Examples

# Make sythetic data
set.seed(1)
m <- 8
n <- 10
x <- seq(m)
y <- seq(n)
z <- matrix(runif(m*n), nrow=m, ncol=n)

# Ex 1 - add another image layer
image(x, y, z, col=grey.colors(20))
N <- sample(1:(m*n),20)
z2 <- NaN*z
z2[N] <- 1
image(x, y, z2, col=rgb(0,0,1,0.4), add=TRUE)
box()

# Ex 2 - add polygons
image(x, y, z, col=grey.colors(20))
poly <- matrixPoly(x, y, z=z, n=N)
sapply(poly, function(X){polygon(X, col=rgb(1,1,0,0.4), border=1)})
box()

# Ex 3 - add polygons to unequal grid
x2 <- cumsum(round(runif(m, min=1, max=10)))
y2 <- cumsum(round(runif(n, min=1, max=10)))
image(x2, y2, z, col=grey.colors(20))
poly <- matrixPoly(x2, y2, z=z, n=N)
sapply(poly, function(X){polygon(X, col=rgb(1,0,0,0.4), border=1)})
box()

# Ex 4 - volcano with snow
image(volcano, col=jetPal(20))
poly <- matrixPoly(z=volcano, n=which(volcano > 175)) # snow line = 180
sapply(poly, function(X){polygon(X, col=rgb(1,1,1,0.75), border=NA)})


marchtaylor/sinkr documentation built on July 4, 2022, 5:48 p.m.