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
|
|
|
|
#' @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}}
|
|
|
|
#'
|
|
|
|
#' @export
|
2021-12-09 12:21:38 +00:00
|
|
|
matrixImage <- function(A, main = NULL, sub = NULL, interpolate = FALSE, ...) {
|
2021-12-07 14:03:47 +00:00
|
|
|
|
|
|
|
# Scale values of `A` to [0, 1] with min mapped to 1 and max to 0.
|
|
|
|
A <- (max(A) - A) / diff(range(A))
|
|
|
|
|
|
|
|
# plot raster image
|
|
|
|
plot(c(0, ncol(A)), c(0, nrow(A)), type = "n", bty = "n", col = "red",
|
2021-12-09 12:21:38 +00:00
|
|
|
xlab = "", ylab = "", xaxt = "n", yaxt = "n", main = main, sub = sub)
|
2021-12-07 14:03:47 +00:00
|
|
|
|
|
|
|
# Add X-axis giving index
|
|
|
|
ind <- seq(1, ncol(A), by = 1)
|
|
|
|
axis(1, at = ind - 0.5, labels = ind, lwd = 0, lwd.ticks = 1)
|
|
|
|
# Add Y-axis
|
|
|
|
ind <- seq(1, nrow(A))
|
|
|
|
axis(2, at = ind - 0.5, labels = rev(ind), lwd = 0, lwd.ticks = 1, las = 1)
|
|
|
|
|
|
|
|
rasterImage(A, 0, 0, ncol(A), nrow(A), interpolate = interpolate, ...)
|
|
|
|
}
|