| Title: | Draw Images of Raster Data and Related Adornments |
|---|---|
| Description: | Draw images easily, set up a plot with an image, specify where that image should be placed. Image plot by default reflects the index of the image data itself, or can be specified in simple extent terms 'xmin,xmax,ymin,ymax'. Numeric matrices, integer arrays, byte arrays, character arrays, and native rasters are supported. Flat vectors are supported in a limited sense with a heuristic to guess at the likely dimensionality and some helpers. A combination of image() and rasterImage() from the 'graphics' package with their good features in one place. |
| Authors: | Michael D. Sumner [aut, cre, cph], Chris Toney [ctb] |
| Maintainer: | Michael D. Sumner <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0.9002 |
| Built: | 2026-10-06 08:55:55 UTC |
| Source: | https://github.com/hypertidy/ximage |
Data obtained from png package.
logo_n A matrix of colour values in native raster form.
logo_a An array of RGB colour values.
ximage(logo_n, asp = .3) ximage(logo_a, extent = c(8, 18, 60, 80), add = TRUE) rect(8, 60, 18, 80)ximage(logo_n, asp = .3) ximage(logo_a, extent = c(8, 18, 60, 80), add = TRUE) rect(8, 60, 18, 80)
Data obtained from GEBCO 2019, via whatarelief package from AAD COG (GeoTIFF).
A matrix of global elevation values in a smallish matrix, extent is -180,180,-90,90, crs is "OGC:CRS84".
ximage(topo, extent = c(-180, 180, -90, 90)) ximage(logo_n, extent = c(135, 155, -48, -30), add = TRUE)ximage(topo, extent = c(-180, 180, -90, 90)) ximage(logo_n, extent = c(135, 155, -48, -30), add = TRUE)
Like contour() but to work with ximage()
xcontour(x, extent = NULL, ..., add = FALSE)xcontour(x, extent = NULL, ..., add = FALSE)
x |
something we can contour |
extent |
optional, numeric xmin,xmax,ymin,ymax |
... |
Arguments passed on to
|
add |
add to plot, or start afresh |
Input may be a matrix or a list from gdal_raster_data() in the vapour package or from read_ds()
in the gdalraster package.
nothing, called for its side effect of creating or adding to a plot
#EPSG:27200 see https://github.com/mdsumner/volcano ex <- c(2667400, 2668010, 6478700, 6479570) v <- volcano[nrow(volcano):1, ncol(volcano):1] ximage(v, extent = ex, asp = 1) xcontour(v, add = TRUE, extent = ex, col = "white") xrect(ex, add = TRUE, border = "hotpink", lwd = 5)#EPSG:27200 see https://github.com/mdsumner/volcano ex <- c(2667400, 2668010, 6478700, 6479570) v <- volcano[nrow(volcano):1, ncol(volcano):1] ximage(v, extent = ex, asp = 1) xcontour(v, add = TRUE, extent = ex, col = "white") xrect(ex, add = TRUE, border = "hotpink", lwd = 5)
ximage combines the best of graphics::image() and graphics::rasterImage().
ximage( x, extent = NULL, zlim = NULL, add = FALSE, ..., xlab = NULL, ylab = NULL, col = hcl.colors(96, "YlOrRd", rev = TRUE), breaks = NULL, alpha = NULL, na.col = "transparent" ) ## S3 method for class 'raw' ximage( x, extent = NULL, zlim = NULL, add = FALSE, ..., xlab = NULL, ylab = NULL, col = hcl.colors(96, "YlOrRd", rev = TRUE), breaks = NULL, alpha = NULL, na.col = "transparent", force = FALSE ) ## S3 method for class 'numeric' ximage( x, extent = NULL, zlim = NULL, add = FALSE, ..., xlab = NULL, ylab = NULL, col = hcl.colors(96, "YlOrRd", rev = TRUE), breaks = NULL, alpha = NULL, na.col = "transparent", force = FALSE ) ## S3 method for class 'integer' ximage( x, extent = NULL, zlim = NULL, add = FALSE, ..., xlab = NULL, ylab = NULL, col = hcl.colors(96, "YlOrRd", rev = TRUE), breaks = NULL, alpha = NULL, na.col = "transparent", force = FALSE ) ## S3 method for class 'character' ximage( x, extent = NULL, zlim = NULL, add = FALSE, ..., xlab = NULL, ylab = NULL, col = hcl.colors(96, "YlOrRd", rev = TRUE), breaks = NULL, alpha = NULL, na.col = "transparent", force = FALSE )ximage( x, extent = NULL, zlim = NULL, add = FALSE, ..., xlab = NULL, ylab = NULL, col = hcl.colors(96, "YlOrRd", rev = TRUE), breaks = NULL, alpha = NULL, na.col = "transparent" ) ## S3 method for class 'raw' ximage( x, extent = NULL, zlim = NULL, add = FALSE, ..., xlab = NULL, ylab = NULL, col = hcl.colors(96, "YlOrRd", rev = TRUE), breaks = NULL, alpha = NULL, na.col = "transparent", force = FALSE ) ## S3 method for class 'numeric' ximage( x, extent = NULL, zlim = NULL, add = FALSE, ..., xlab = NULL, ylab = NULL, col = hcl.colors(96, "YlOrRd", rev = TRUE), breaks = NULL, alpha = NULL, na.col = "transparent", force = FALSE ) ## S3 method for class 'integer' ximage( x, extent = NULL, zlim = NULL, add = FALSE, ..., xlab = NULL, ylab = NULL, col = hcl.colors(96, "YlOrRd", rev = TRUE), breaks = NULL, alpha = NULL, na.col = "transparent", force = FALSE ) ## S3 method for class 'character' ximage( x, extent = NULL, zlim = NULL, add = FALSE, ..., xlab = NULL, ylab = NULL, col = hcl.colors(96, "YlOrRd", rev = TRUE), breaks = NULL, alpha = NULL, na.col = "transparent", force = FALSE )
x |
matrix, array, raw or character matrix, native raster (nativeRaster, or raster), or list as output by GDAL reader functions |
extent |
optional, numeric xmin,xmax,ymin,ymax |
zlim |
optional, absolute range of data to map colours to (maintains comparable colours across plots); values outside display as 'na.col'; single-band numeric data only |
add |
add to plot, or start afresh |
... |
passed to plot when |
xlab |
x axis label, empty by default |
ylab |
y axis label, empty by default |
col |
colours to map single-band data to |
breaks |
a set of finite numeric breakpoints for the colours, one more break than colour (if not, colours are interpolated to fit) |
alpha |
optional constant opacity in |
na.col |
colour for missing values, default "transparent" |
force |
proceed with a guessed dimension for very long bare vectors
(see Details), default |
ximage() is a combination those graphics function with the the best features in one.
Allow arrays with RGB/A.
Allow matrix with character (named colours, or hex) or raw (Byte) values
Allow list output from vapour or gdalraster, a list with numeric values, hex character, or nativeRaster
Plot in 0,ncol 0,nrow by default
Override default with extent (xmin, xmax, ymin, ymax)
Allow general numeric values.
Start a plot from scratch without setting up a plot to paint to.
Plot by default in 0,ncol,0,nrow if unspecified.
Data orientation is "raster order", the first cell is the top-left of the displayed image, following scan lines down the page (see the package vignette on orientation).
Bare atomic vectors (no dim, no 'gis' attribute from gdalraster, no
width/height/depth attributes) are accepted and a dimension is guessed
by integer-division detection of the vector length. The most nearly
square factorization is chosen (preferring landscape, ncol >= nrow) and
the data is assumed to be in raster scanline order, i.e. flat output
from GDAL readers such as vapour::gdal_raster_data() or
gdalraster::read_ds(), built as matrix(x, ncol = NC, byrow = TRUE);
column-major R data needs dim(x) <- c(NR, NC) instead. A message
reports the guess and the candidate factorizations from 1xN through
Nx1, in GDAL dimension order (ncol x nrow, xsize x ysize). Raw
vectors also consider 3-plane (RGB) and 4-plane (RGBA) scanline
pixel-interleaved interpretations; numeric data never gets a plane
interpretation (whole numbers in 0..255 are too common as ordinary
data to imply an image, a much narrower opt-in detection may come
later). For very long vectors (more than
getOption("ximage.guess_max", 2^24) elements) the guess stops with
an error unless force = TRUE, or set the dimension explicitly.
Colour mapping via 'col', 'breaks', and 'zlim' applies to single-band numeric data only. Multi-band (grey/alpha, RGB, RGBA) data is scaled automatically: values within 0,1 are used as-is, within 0,255 are divided by 255, and anything else is rescaled by the finite range of the colour bands. Missing values (NA, NaN) display as 'na.col' in all cases.
invisibly, a list with 'x' (the colour data as plotted) and 'extent' (xmin, xmax, ymin, ymax used, the 0,ncol 0,nrow index space of the input if not supplied)
ximage(volcano) ximage(as.raster(matrix(0:1, 49, 56))) v <- volcano v[v > 180] <- NA ximage(v, na.col = "hotpink") ## bare vectors in GDAL scanline order get a guessed dimension, with a ## message listing candidate shapes (ncol x nrow); here the guess 87x61 ## is the mirror of the true 61x87, so pick from the candidates ximage(as.vector(t(volcano))) ximage(matrix(as.vector(t(volcano)), ncol = 61, byrow = TRUE))ximage(volcano) ximage(as.raster(matrix(0:1, 49, 56))) v <- volcano v[v > 180] <- NA ximage(v, na.col = "hotpink") ## bare vectors in GDAL scanline order get a guessed dimension, with a ## message listing candidate shapes (ncol x nrow); here the guess 87x61 ## is the mirror of the true 61x87, so pick from the candidates ximage(as.vector(t(volcano))) ximage(matrix(as.vector(t(volcano)), ncol = 61, byrow = TRUE))
Draw rectangles from four colummns xmin,xmax,ymin,ymax
xrect(x, add = FALSE, ..., asp = 1L)xrect(x, add = FALSE, ..., asp = 1L)
x |
four columns worth of rectangles |
add |
instantiate a plot or add to existing (default is |
... |
arguments passed to |
asp |
aspect ratio, defaults to 1 |
Calls rect(), but will instantiate a plot if add = FALSE.
nothing, called for side effect of creating or adding to a plot
xrect(runif(100)) xrect(sort(runif(100))) xrect(runif(100), col = hcl.colors(25, alpha = seq(.2, .8, length.out = 25))) ex <- c(0.2, 0.8, .2, .6) xrect(ex, add = TRUE, lwd = 5, lty = 2)xrect(runif(100)) xrect(sort(runif(100))) xrect(runif(100), col = hcl.colors(25, alpha = seq(.2, .8, length.out = 25))) ex <- c(0.2, 0.8, .2, .6) xrect(ex, add = TRUE, lwd = 5, lty = 2)
Draw the values of a matrix as text labels, each at the centre of its cell
in the same layout used by ximage(). The first cell of the matrix is the
top-left label, and each row of the matrix is a line of labels reading
left to right down the page ("raster order").
xtext(x, extent = NULL, add = FALSE, cex = NULL, fit = 0.9, ...)xtext(x, extent = NULL, add = FALSE, cex = NULL, fit = 0.9, ...)
x |
a matrix of values to draw as labels, or a reader-output list |
extent |
optional, numeric xmin,xmax,ymin,ymax (defaults to the 0,ncol 0,nrow index space of the matrix) |
add |
add to plot, or start afresh |
cex |
text size, computed to fit the cells if not supplied |
fit |
fraction of the cell the largest label should occupy when 'cex' is computed, default 0.9 |
... |
arguments passed to |
Text size is chosen automatically to fit the largest label within a cell, scaled by 'fit' (use 'cex' to override). Labels that are NA are not drawn.
Input may be a matrix (values are used as labels via format()), or a
list in the style of vapour or gdalraster reader output (a row-major
vector with 'dimension'/'extent' attributes, or a 'gis' attribute).
invisibly, a list with 'x', 'y' (label positions), 'labels', and 'cex' as used
m <- matrix(1:12, 3, byrow = TRUE) ximage(m) xtext(m, add = TRUE) ## the label positions are cell centres in the extent ximage(volcano, extent = c(0, 1, 0, 1)) xtext(volcano[seq(1, 87, by = 8), seq(1, 61, by = 6)], extent = c(0, 1, 0, 1), add = TRUE, col = "white")m <- matrix(1:12, 3, byrow = TRUE) ximage(m) xtext(m, add = TRUE) ## the label positions are cell centres in the extent ximage(volcano, extent = c(0, 1, 0, 1)) xtext(volcano[seq(1, 87, by = 8), seq(1, 61, by = 6)], extent = c(0, 1, 0, 1), add = TRUE, col = "white")