Skip to contents

Helper function that converts numeric values to colors and returns a raster image. Useful for visualizing numeric matrices as color backgrounds.

Usage

as_colorraster(x, palette = hcl.colors(256), na.color = "white")

Arguments

x

A numeric vector or matrix containing values to be mapped to colors.

palette

A vector of colors used as the palette. By default, viridisLite::viridis(256) is used.

na.color

Color used for NA values. Defaults to NA.

Value

A raster object as produced by as.raster().

Details

Values in x are rescaled to the range of the palette using scales::rescale(), and each value is mapped to a corresponding color. If x is a matrix, the resulting raster preserves the same dimensions.

Examples

# Convert the volcano matrix to a color raster
img <- as_colorraster(volcano)
plot(img)


# Use a different color palette
img <- as_colorraster(volcano, palette = terrain.colors(256))
plot(img)