| 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 (or will be) supported. 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 |
| Built: | 2026-07-21 14:53:47 UTC |
| Source: | https://github.com/cran/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" )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" )
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" |
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).
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")ximage(volcano) ximage(as.raster(matrix(0:1, 49, 56))) v <- volcano v[v > 180] <- NA ximage(v, na.col = "hotpink")
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")