| Title: | Calibrated Color Ramps |
|---|---|
| Description: | Value-calibrated color ramps can be useful to emphasize patterns in data from complex distributions. Colors can be tied to specific values, and the association can be expanded into full color ramps that also include the relationship between colors and values. Such ramps can be used in a variety of cases when heatmap-type plots are necessary, including the visualization of vector and raster spatial data, such as topographies. |
| Authors: | Adam T. Kocsis [cre, aut] (ORCID: <https://orcid.org/0000-0002-9028-665X>), Deutsche Forschungsgemeinschaft [fnd], FAU GeoZentrum Nordbayern [fnd] |
| Maintainer: | Adam T. Kocsis <[email protected]> |
| License: | CC BY 4.0 |
| Version: | 0.2.0 |
| Built: | 2026-06-04 07:27:42 UTC |
| Source: | https://github.com/adamkocsis/rampage |
The function is a wrapper around the points function, controlling the color of the points similar to ggplot, but using S-style plotting.
If neither ramp, nor col or breaks are given, the function will default to using the internal gradinv palette with 256 levels evenly distributed from the minimum to the maximum of z.
colorpoints( x, y = NULL, z, ramp = NULL, col = NULL, breaks = NULL, legend = list(x = "topleft"), ... )colorpoints( x, y = NULL, z, ramp = NULL, col = NULL, breaks = NULL, legend = list(x = "topleft"), ... )
x |
The |
y |
The |
z |
|
ramp |
A |
col |
A vector of colors. Used only if |
breaks |
A vector of breaks. If given, this has to be one element longer than the length of |
legend |
A list of arguments passed to the |
... |
Arguments passed to the |
The function has no return value.
# random points set.seed(1) x <- rnorm(5000) # x coord y <- rnorm(5000) # y coord dist <- sqrt(x^2+y^2) # distance from origin # default plotting plot(x,y, col=NA) colorpoints(x=x, y=y, z=dist) # custom color scheme levs<- data.frame(color=rainbow(5), z=c(0, 0.5,1, 3, 4.5)) ramp <-expand(levs, n=256) # very customized (experiment with difference device sizes!) plot(x,y, col=NA, main="Distance to origin") colorpoints(x=x, y=y, z=dist, col=paste0(ramp$col, "BB"), breaks=ramp$breaks, pch=16, legend=list(x=3,y=0,cex=0.7, box.args=list(border=NA)))# random points set.seed(1) x <- rnorm(5000) # x coord y <- rnorm(5000) # y coord dist <- sqrt(x^2+y^2) # distance from origin # default plotting plot(x,y, col=NA) colorpoints(x=x, y=y, z=dist) # custom color scheme levs<- data.frame(color=rainbow(5), z=c(0, 0.5,1, 3, 4.5)) ramp <-expand(levs, n=256) # very customized (experiment with difference device sizes!) plot(x,y, col=NA, main="Distance to origin") colorpoints(x=x, y=y, z=dist, col=paste0(ramp$col, "BB"), breaks=ramp$breaks, pch=16, legend=list(x=3,y=0,cex=0.7, box.args=list(border=NA)))
Create ramp palettes from fixed color positions.
expand(x = NULL, n, color = "color", z = "z", ...)expand(x = NULL, n, color = "color", z = "z", ...)
x |
A |
n |
A single |
color |
A |
z |
A |
... |
Arguments passed to the |
The function creates objects of the S3 class calibramp. The calibramp-class lists have three elements: col hexadecimal color values, mid: z-values of midpoints (one for every color), and breaks: separator borders between color values.
Color interpolation will be executed linearly, using colorRampPalette, the order of the values will be forced to ascending, the values in mid will be halfway between breaks.
A calibramp-class object (see description above).
library(rampage) data(topos) ramp <- expand(topos$havanna2, n=200) plot(ramp)library(rampage) data(topos) ramp <- expand(topos$havanna2, n=200) plot(ramp)
SpatRaster objectShorthand for limiting maximum and minimum values in a SpatRaster-class object.
limit(x, y = NULL, min = NULL, max = NULL)limit(x, y = NULL, min = NULL, max = NULL)
x |
Object to limit, a |
y |
Either a range (i.e. a |
min |
If |
max |
If |
A SpatRaster object.
# This function relies on the terra extension if(requireNamespace("terra", quietly=TRUE)){ library(terra) # Example 1. Using specific values # a SpatRaster object r<- terra::rast() # populate with a Gaussian distribution terra::values(r) <- rnorm(terra::ncell(r), 0.5,1 ) # and limit rLimit <- limit(r, min=-0.2, max=0.2) plot(rLimit) # Example 2. Using an expanded color ramp # Create a data.frame df <- data.frame( z=c(-1, -0.2, 0, 0.2, 1), color=rev(gradinv(5)) ) ramp <- expand(df, n=200) rLimited <- limit(r, y=ramp) # default plot(rLimited) # manual ramping. plot(rLimited, breaks=ramp$breaks, col=ramp$col, legend=FALSE) # temporary solution for manual legend # Marginal ramps will be implemented later ramplegend(x=140, y=90, ramp=ramp, cex=0.5, at=c(-1, 0, 1), label=c("< -1", 0, "> +1")) }# This function relies on the terra extension if(requireNamespace("terra", quietly=TRUE)){ library(terra) # Example 1. Using specific values # a SpatRaster object r<- terra::rast() # populate with a Gaussian distribution terra::values(r) <- rnorm(terra::ncell(r), 0.5,1 ) # and limit rLimit <- limit(r, min=-0.2, max=0.2) plot(rLimit) # Example 2. Using an expanded color ramp # Create a data.frame df <- data.frame( z=c(-1, -0.2, 0, 0.2, 1), color=rev(gradinv(5)) ) ramp <- expand(df, n=200) rLimited <- limit(r, y=ramp) # default plot(rLimited) # manual ramping. plot(rLimited, breaks=ramp$breaks, col=ramp$col, legend=FALSE) # temporary solution for manual legend # Marginal ramps will be implemented later ramplegend(x=140, y=90, ramp=ramp, cex=0.5, at=c(-1, 0, 1), label=c("< -1", 0, "> +1")) }
The elevation to color bindings are the work of C. Scotese. Data from Scotese, C. R., Vérard, C., Burgener, L., Elling, R. P., & Kocsis, A. T. (2025). The Cretaceous World: Plate Tectonics, Paleogeography, and Paleoclimate. Geological Society, London, Special Publications, 544(1), SP544–2024..
data(paleomap)data(paleomap)
A calibramp-class list with 3 numerics:
col: The color levels as hexadecimal RGB values.
breaks: The boundaries for the individual levels.
mid: The mid values of the color levels.
https://zenodo.org/records/10659112
The method can be used to inspect and visualize calbirated color ramp object.
## S3 method for class 'calibramp' plot(x, ...) rampplot( x, breaks = FALSE, breaklabs = TRUE, axis.args = list(side = 2), ylab = "z", xlab = "" )## S3 method for class 'calibramp' plot(x, ...) rampplot( x, breaks = FALSE, breaklabs = TRUE, axis.args = list(side = 2), ylab = "z", xlab = "" )
x |
The calibirated color ramp object ( |
... |
Arguments passed to the |
breaks |
Should the distribution of breaks be visualized? |
breaklabs |
Should the minimum and maximum break labels be visualized? |
axis.args |
Arguments passed to the axis function. |
ylab |
y-axis label. |
xlab |
x-axis label. |
The functions have no return values.
# the paleomap ramp data(paleomap) plot(paleomap) # 0-calibrated, expanded ramp tiepoints <- data.frame(z=c(c(-1, -0.1, 0, 0.1, +1)), color=gradinv(5)) ramp <- expand(tiepoints, n=255) plot(ramp)# the paleomap ramp data(paleomap) plot(paleomap) # 0-calibrated, expanded ramp tiepoints <- data.frame(z=c(c(-1, -0.1, 0, 0.1, +1)), color=gradinv(5)) ramp <- expand(tiepoints, n=255) plot(ramp)
Value-calibrated color ramps can be useful to emphasize patterns in weirdly distributed data. Tiepoints can be used to fix colors to specific values, and the resulting association can then be expanded into full color ramps. Such ramps can be used a variety of cases when heatmap type plotting is necessary, including the visualization of raster and polygon spatial data.
This is still a Beta version. As is R, this is free software and comes with ABSOLUTELY NO WARRANTY. Nevertheless, notes about found bugs and suggestions are more than welcome.
Adam T. Kocsis ([email protected])
Useful links:
Optional legend for cases where calibramp objects are used.
ramplegend( x = "topleft", y = NULL, shift = c(0, 0), ramp = NULL, col = NULL, breaks = NULL, zlim = NULL, height = 3, width = 0.3, tick.length = 0.15, cex = 1, box.args = list(col = "#ffffffbb"), horizontal = FALSE, at = NULL, label = NULL )ramplegend( x = "topleft", y = NULL, shift = c(0, 0), ramp = NULL, col = NULL, breaks = NULL, zlim = NULL, height = 3, width = 0.3, tick.length = 0.15, cex = 1, box.args = list(col = "#ffffffbb"), horizontal = FALSE, at = NULL, label = NULL )
x |
Position of the legend or the left coordinate of legend box. Either a numeric coordinate value or a the |
y |
Coordinate of the upper edge of the legend (if needed). |
shift |
Used instead of the inset argument of the default legend. If plotted within the inner plotting area, the x and y user coordinates with which the position of the legend will be shifted. |
ramp |
A calibrated color ramp object. Either |
col |
Vector of colors. |
breaks |
Breaks between the colors. |
zlim |
A numeric vector with two values, passed to |
height |
Height of the legend bar in inches. |
width |
Width of the legend bar in inches. |
tick.length |
The length of the legend's ticks. |
cex |
Legend size scaler. |
box.args |
the box's arguments. |
horizontal |
Legend orientation. Not yet implemented |
at |
Where should the legend be drawn in the z dimension? |
label |
What are the labels |
The function has no return value.
# example with colored points # basic points v <- seq(0,20, 0.01) sine <- sin(v) # visualize as a plot plot(v,sine) # colors for sine values levs<- data.frame(color=gradinv(5), z=c(-1, -0.2, 0, 0.2, 1)) ramp<- expand(levs, n=256) # colored points colorpoints(x=v, y=sine, z=sine, cex=6, pch=16, legend=NULL) # the legend ramplegend(x=0, y=0.3, ramp=ramp, cex=0.5, box.args=list(border=NA, col=NA)) # example with histogram set.seed(1) x <- rnorm(3000, 3,1) levs<- data.frame(color=gradinv(7), z=c(-1, 1,1.04, 3, 4.96, 5, 7)) ramp <-expand(levs, n=400) # histogram showing distribution hist(x, col=ramp$col, breaks=ramp$breaks, border=NA) ramplegend("topleft", ramp=ramp, at=c(1.04, 3, 4.96), label=c("-1.96 SD", "mean", "+1.96 SD")) # example with volcano data(volcano) data(topos) # create ramp levs <- topos$jakarta[topos$jakarta$z>0,] levs$z <- c(200, 180, 165, 130, 80) ramp <-expand(levs, n=100) image(volcano, col=ramp$col, breaks=ramp$breaks) ramplegend(x=0.8, y=0.8, ramp=ramp, cex=0.9)# example with colored points # basic points v <- seq(0,20, 0.01) sine <- sin(v) # visualize as a plot plot(v,sine) # colors for sine values levs<- data.frame(color=gradinv(5), z=c(-1, -0.2, 0, 0.2, 1)) ramp<- expand(levs, n=256) # colored points colorpoints(x=v, y=sine, z=sine, cex=6, pch=16, legend=NULL) # the legend ramplegend(x=0, y=0.3, ramp=ramp, cex=0.5, box.args=list(border=NA, col=NA)) # example with histogram set.seed(1) x <- rnorm(3000, 3,1) levs<- data.frame(color=gradinv(7), z=c(-1, 1,1.04, 3, 4.96, 5, 7)) ramp <-expand(levs, n=400) # histogram showing distribution hist(x, col=ramp$col, breaks=ramp$breaks, border=NA) ramplegend("topleft", ramp=ramp, at=c(1.04, 3, 4.96), label=c("-1.96 SD", "mean", "+1.96 SD")) # example with volcano data(volcano) data(topos) # create ramp levs <- topos$jakarta[topos$jakarta$z>0,] levs$z <- c(200, 180, 165, 130, 80) ramp <-expand(levs, n=100) image(volcano, col=ramp$col, breaks=ramp$breaks) ramplegend(x=0.8, y=0.8, ramp=ramp, cex=0.9)
Contains functions produced by the colorRampPalette function.
gradinv(n)gradinv(n)
n |
( |
You can also view single palettes individually. The following color palettes are implemented:
gradinv(): inverse heatmap, primarily intended to emphasize distinctions between no-change (yellow) and change (blue/red) cases.
Based on the color blindness simulator Coblis, the palette is very color disability-friendly, except for monochromacy/achromatopsia, where the two change scenarios (red and blue) become very difficult to distinguish.
Additional markings (e.g. labels indicating high and low) are recommended for these cases.
A character vector of color values.
cols <- gradinv(20) plot(1:20, col=cols, pch=16, cex=2)cols <- gradinv(20) plot(1:20, col=cols, pch=16, cex=2)
The object contains data.frame-class objects to be used with the expand function to produce full calibrated color ramps.
data(topos)data(topos)
A list with 6 data.frame elements:
demcmap: The "demcmap" theme, based on MatLab's demcmap.
etopo: The "etopo" theme, approximate elevation-color assignments based on the the ETOPO Global Relief Model poster (https://www.ncei.noaa.gov/media/3340).
jakarta: The "Jakarta" theme, color values by Deviantart user Arcanographia.
havanna2: The "Havanna-2" theme, color values by Deviantart user Arcanographia.
tokio1: The "Tokio-1" theme, color values by Deviantart user Arcanographia.
zagreb: The "Zagreb" theme, color values by Deviantart user Arcanographia.
data(topos) jakExp <- expand(topos$jakarta, n=200) plot(jakExp)data(topos) jakExp <- expand(topos$jakarta, n=200) plot(jakExp)
Modify the minimum and maximum values in a calibramp-class object produced by the expand function.
trimramp(x, low = NULL, high = NULL)trimramp(x, low = NULL, high = NULL)
x |
A calibrated color ramp (e.g. |
low |
A single |
high |
A single |
A trimmed version of x, another calibramp-class object.
data(paleomap) trimmed <- trimramp(paleomap, low=-500, high=1500) plot(trimmed)data(paleomap) trimmed <- trimramp(paleomap, low=-500, high=1500) plot(trimmed)