Print a rudimentary image of a matrix on the R console using a user-supplied alphabet as a palette of sorts.
txtimage(z, width, height, yaxis = c('up', 'down'), transpose = TRUE,
legend = TRUE, na.char = ' ', alphabet = 0:9, Lanczos = 3)
Numeric matrix containing values to be plotted. NA
and
NaN
are allowed, but infinities, being impossible to scale,
cause an error.
Desired width in characters. Defaults to full screen (by means of
getOption('width')
) or number of columns (rows if transposed)
in z
, whichever is less. Asking for more characters than
there is in the supplied matrix results in an error.
Desired height in characters. Defaults to \(25/80\) of width
or number of rows (columns if transposed) in z
, whichever
is less. Asking for more characters than there is in the supplied
matrix results in an error.
Direction of the Y axis, "up"
or "down"
. Defaults to
'up' like in image
.
Whether to arrange rows by the X axis, like image
does. Defaults to TRUE
.
Whether to print the legend under the plot. If set, the returned
object will have the 'cuts'
and 'alphabet'
attributes
set containing the values separating the intervals and characters
used for the intervals, respectively.
Character to substitute for values satisfying is.na
. A warning
is produced if na.char
is found in the alphabet
in
presence of NA
in z
.
Symbols to compose the plot of, linear scale. Either a single
multi-character string or a vector of single-character strings.
Defaults to 0:9
.
Positive integer defining the size of the Lanczos filter kernel.
Given a value of \(a\), the windowed sinc kernel will have
\(2a-1\) lobes. Increasing the value may lead to better frequency
response, but cause worse performance and wider undefined zones when
the input contains NA
s.
The function is called for its side effect of printing the textual
plot on the R console using cat
, but it also invisibly returns
the resulting character matrix. If legend
is TRUE
, the
'cuts'
attribute contains the values separating the intervals
used for characters in the alphabet
(the copy of which is stored
in the 'alphabet'
attribute).
By default, txtimage
mimics the behaviour of image
,
drawing the rows of the matrix along the X axis and making the Y axis
grow from bottom to the top of the plot. The function can be made to
mimic matrix print
instead (rows arranged vertically from top to
bottom) by specifying yaxis
and image.transpose
arguments.
If requested width
or height
is different from dimensions
of the matrix, it is resampled using the Lanczos filter for a given
downsampling ratio \(r\) and window parameter \(a\):
$$% L(x) = \mathrm{sinc}(x) \, \mathrm{sinc}(x/a) \, | x | < a $$ $$% S_{ij} = \sum_{k, l} s_{kl} % L \left( i - \frac{k}{r} \right) % L \left( j - \frac{l}{r} \right) $$
When resampling, the rows and columns are assumed to correspond to a uniform linear grid.
Szeliski, R. (2010) Computer Vision: Algorithms and Applications. Section 3.5.2: Decimation.
symnum
, image
# NOT RUN {
txtimage(datasets::volcano)
# }
# NOT RUN {
## try this if your terminal supports shade/block characters
txtimage(datasets::volcano, alphabet = " \u2591\u2592\u2593\u2588")
# }
Run the code above in your browser using DataLab