matrixPoly | R Documentation |
matrixPoly
creates a list of polygon coordinates given a matrix z
and
corresponding x
and y
coordinates for the dimensions of z
.
matrixPoly(x, y, z, n = NULL)
x, y |
Optional vectors of values for matrix |
z |
A matrix |
n |
An optional vector of element positions of |
List of polygon coordinates for each element of z
# 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)})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.