add: plot_matrix

This commit is contained in:
Daniel Kapla 2021-12-07 15:03:47 +01:00
parent 9123baccdd
commit 5f36ec21de
1 changed files with 27 additions and 0 deletions

View File

@ -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, ...)
}