tensor_predictors/tensorPredictors/R/matrixImage.R

29 lines
1.0 KiB
R

#' Plots a matrix as an image
#'
#' @param A a matrix to be plotted
#' @param main overall title for the plot
#' @param sub sub-title of the plot
#' @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
matrixImage <- function(A, main = NULL, sub = NULL, interpolate = FALSE, ...) {
# 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",
xlab = "", ylab = "", xaxt = "n", yaxt = "n", main = main, sub = sub)
# 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, ...)
}