diff --git a/tensorPredictors/R/plot_matrix.R b/tensorPredictors/R/plot_matrix.R new file mode 100644 index 0000000..46bf818 --- /dev/null +++ b/tensorPredictors/R/plot_matrix.R @@ -0,0 +1,27 @@ +#' Plots a matrix an image +#' +#' @param A a matrix to be plotted +#' @param main overall title for 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 +plot.matrix <- function(A, main = 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) + + # 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, ...) +}