2021-12-09 12:21:38 +00:00
|
|
|
#' Plots a matrix as an image
|
2021-12-07 14:03:47 +00:00
|
|
|
#'
|
|
|
|
#' @param A a matrix to be plotted
|
2022-01-13 10:13:34 +00:00
|
|
|
#' @param add.values boolean indicating if matrix values are to be written into
|
|
|
|
#' matrix element boxes
|
2021-12-07 14:03:47 +00:00
|
|
|
#' @param main overall title for the plot
|
2021-12-09 12:21:38 +00:00
|
|
|
#' @param sub sub-title of the plot
|
2021-12-07 14:03:47 +00:00
|
|
|
#' @param interpolate a logical vector (or scalar) indicating whether to apply
|
|
|
|
#' linear interpolation to the image when drawing.
|
|
|
|
#' @param ... further arguments passed to \code{\link{rasterImage}}
|
|
|
|
#'
|
2022-01-13 10:13:34 +00:00
|
|
|
#' @examples
|
|
|
|
#' AR <- 0.5^abs(outer(1:10, 1:10, `-`))
|
|
|
|
#' matrixImage(AR, AR > 0.2, main = "Autoregressiv Covariance")
|
|
|
|
#'
|
2021-12-07 14:03:47 +00:00
|
|
|
#' @export
|
2022-01-13 10:13:34 +00:00
|
|
|
matrixImage <- function(A, add.values = FALSE,
|
2022-10-31 14:14:58 +00:00
|
|
|
main = NULL, sub = NULL, interpolate = FALSE, ..., zlim = NA,
|
|
|
|
axes = TRUE, asp = 1, col = hcl.colors(24, "YlOrRd", rev = FALSE),
|
2022-03-22 15:26:24 +00:00
|
|
|
digits = getOption("digits")
|
2022-01-13 10:13:34 +00:00
|
|
|
) {
|
2021-12-07 14:03:47 +00:00
|
|
|
# plot raster image
|
2022-10-31 14:14:58 +00:00
|
|
|
plot(c(0, ncol(A)), c(0, nrow(A)), type = "n", bty = "n", col = "black",
|
|
|
|
xlab = "", ylab = "", xaxt = "n", yaxt = "n", main = main, sub = sub,
|
|
|
|
asp = asp)
|
2021-12-07 14:03:47 +00:00
|
|
|
|
2022-01-13 10:13:34 +00:00
|
|
|
# Scale values of `A` to [0, 1] with min mapped to 1 and max to 0.
|
2022-10-31 14:14:58 +00:00
|
|
|
if (missing(zlim)) {
|
|
|
|
S <- (max(A) - A) / diff(range(A))
|
|
|
|
} else {
|
|
|
|
S <- pmin(pmax(0, (zlim[2] - A) / diff(zlim)), 1)
|
|
|
|
}
|
|
|
|
# and not transform to color
|
|
|
|
S <- matrix(col[round((length(col) - 1) * S + 1)], nrow = nrow(A))
|
2022-01-13 10:13:34 +00:00
|
|
|
|
|
|
|
# Raster Image ploting the matrix with element values mapped to grayscale
|
|
|
|
# as big elements (original matrix A) are dark and small (negative) elements
|
|
|
|
# are white.
|
|
|
|
rasterImage(S, 0, 0, ncol(A), nrow(A), interpolate = interpolate, ...)
|
|
|
|
|
2022-10-31 14:14:58 +00:00
|
|
|
# X/Y axes index (matches coordinates to matrix indices)
|
2022-01-13 10:13:34 +00:00
|
|
|
x <- seq(1, ncol(A), by = 1)
|
|
|
|
y <- seq(1, nrow(A))
|
2022-10-31 14:14:58 +00:00
|
|
|
if (axes) {
|
|
|
|
axis(1, at = x - 0.5, labels = x, lwd = 0, lwd.ticks = 1)
|
|
|
|
axis(2, at = y - 0.5, labels = rev(y), lwd = 0, lwd.ticks = 1, las = 1)
|
|
|
|
}
|
2021-12-07 14:03:47 +00:00
|
|
|
|
2022-10-31 14:14:58 +00:00
|
|
|
# Writes matrix values
|
2022-01-13 10:13:34 +00:00
|
|
|
if (any(add.values)) {
|
2022-03-22 15:26:24 +00:00
|
|
|
if (length(add.values) > 1) {
|
|
|
|
A[!add.values] <- NA
|
|
|
|
A[add.values] <- format(A[add.values], digits = digits)
|
|
|
|
}
|
2022-10-31 14:14:58 +00:00
|
|
|
text(rep(x - 0.5, each = nrow(A)), rep(rev(y - 0.5), ncol(A)), A, adj = 0.5)
|
2022-01-13 10:13:34 +00:00
|
|
|
}
|
2021-12-07 14:03:47 +00:00
|
|
|
}
|