Package 'ximage'

Title: Draw Images of Raster Data and Related Adornments
Description: Draw images easily and as if doing that was considered desirable or even essential to be able to do. 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 little bit like 'image()' and 'rasterImage()' from the 'graphics' package but with the good features celebrated and made easier to use.
Authors: Michael D. Sumner [aut, cre], Chris Toney [ctb]
Maintainer: Michael D. Sumner <[email protected]>
License: MIT + file LICENSE
Version: 0.0.0.9014
Built: 2024-08-26 02:11:26 UTC
Source: https://github.com/hypertidy/ximage

Help Index


R logo as an RGB image

Description

Data obtained from png package.

Details

logo_n A matrix of colour values in native raster form.

logo_a An array of RGB colour values.

Examples

ximage(logo_n, asp = .3)
ximage(logo_a, extent = c(8, 18, 60, 80), add = TRUE)
rect(8, 60, 18, 80)

topographic elevation

Description

Data obtained from GEBCO 2019, via whatarelief package from AAD COG (GeoTIFF).

Details

A matrix of global elevation values in a smallish matrix, extent is -180,180,-90,90, crs is "OGC:CRS84".

Examples

ximage(topo, extent = c(-180, 180, -90, 90))
ximage(logo_n, extent = c(135, 155, -48, -30), add = TRUE)

A new contour

Description

To work with ximage()

Usage

xcontour(x, extent = NULL, ..., add = FALSE)

Arguments

x

something we can contour

extent

optional, numeric xmin,xmax,ymin,ymax

...

Arguments passed on to ximage

zlim

optional, range of data to set colour map (to maintain absolute colours across multiple plots)

xlab

x axis label, empty by default

ylab

y axis lable, empty by default

breaks

a set of finite numeric breakpoints for the colours:

col

optional colours to map matrix/array data to

add

add to plot, or start afresh

Details

Input may be a matrix or a list from gdal_raster_data() in the vapour package.

Value

nothing, called for its side effect of creating or adding to a plot

Examples

ex <-  c(2667394, 2668004, 6478902, 6479772)
v <- volcano[nrow(volcano):1, ncol(volcano):1]
ximage(v, extent = ex, asp = 1)
#im <- whatarelief::imagery(extent = ex, projection = "+proj=nzmg +datum=WGS84")
#ximage(im, add = TRUE, extent = ex)
xcontour(v, add = TRUE, extent = ex, col = "white")
xrect(ex, add = TRUE, border = "hotpink", lwd = 5)

A new image() Plot an image (no matter what)

Description

ximage combines the best of image() and rasterImage().

Usage

ximage(
  x,
  extent = NULL,
  zlim = NULL,
  add = FALSE,
  ...,
  xlab = NULL,
  ylab = NULL,
  col = hcl.colors(96, "YlOrRd", rev = TRUE),
  breaks = NULL
)

Arguments

x

matrix, array, or native raster (nativeRaster, or raster)

extent

optional, numeric xmin,xmax,ymin,ymax

zlim

optional, range of data to set colour map (to maintain absolute colours across multiple plots)

add

add to plot, or start afresh

...

passed to plot when add = FALSE

xlab

x axis label, empty by default

ylab

y axis lable, empty by default

col

optional colours to map matrix/array data to

breaks

a set of finite numeric breakpoints for the colours:

Details

ximage() is designed like graphics function with the following limitations removed, image() 1:4, rasterImage() 5:7.

  1. Allow arrays with RGB/A.

  2. Allow matrix with character (named colours, or hex) or raw (Byte) values

  3. Allow list output from vapour, a list with numeric values, hex character, or nativeRaster

  4. Plot in 0,ncol 0,nrow by default

  5. Override default with extent (xmin, xmax, ymin, ymax)

  6. Allow general numeric values.

  7. Start a plot from scratch without setting up a plot to paint to.

  8. Plot by default in 0,ncol,0,nrow if unspecified.

ximage uses the GIS raster default used by rasterImage. WIP: There is a similar function 'image0?' that provides the same features but assumes that orientation is like image...

Colours by 'col' are only mapped for numeric data, this may change (to remap RGB or raw imagery via greyscale conversion)

Value

a list with 'x' and 'extent' invisibly (extent is the 0,ncol 0,nrow space of the array if not supplied)

Examples

ximage(volcano)
ximage(as.raster(matrix(0:1, 49, 56)))

xrect

Description

Draw rectangles from four colummns xmin,xmax,ymin,ymax

Usage

xrect(x, add = FALSE, ..., asp = 1L)

Arguments

x

four columns worth of rectangles

add

instantiate a plot or add to existing (default is add = FALSE)

...

arguments passed to rect()

asp

aspect ratio, defaults to 1

Details

Calls rect(), but will instantiate a plot if add = FALSE.

Value

nothing, called for side effect of creating or adding to a plot

Examples

xrect(runif(100))
xrect(sort(runif(100)))
xrect(runif(100), col = hcl.colors(25, alpha = seq(.2, .8, length.out = 25)))

#example(xcontour, ask = FALSE)
ex <- c(0.2, 0.8, .2, .6)
xrect(ex, add = TRUE, lwd = 5, lty = 2)