| Title: | 'cpt-city' Colour Gradients |
|---|---|
| Description: | Incorporates colour gradients from the 'cpt-city' web archive available at <https://phillips.shef.ac.uk/pub/cpt-city/>. Provides functions to search, preview, and return colour palettes as either interpolated hex vectors or colorRampPalette functions for use with base graphics, ggplot2, and sf. |
| Authors: | Sergio Ibarra-Espinosa |
| Maintainer: | Sergio Ibarra-Espinosa <[email protected]> |
| License: | GPL-3 |
| Version: | 2.3.0 |
| Built: | 2026-07-20 19:23:05 UTC |
| Source: | https://github.com/ibarraespinosa/cptcity |
cpt extracts a named colour palette from the internal cpt-city
database and returns either a character vector of n interpolated
hex colours or the underlying colorRampPalette
function itself. The package ships 7140 colour gradients from
https://phillips.shef.ac.uk/pub/cpt-city/.
cpt( pal = "mpl_inferno", n = 100, colorRampPalette = FALSE, rev = FALSE, frgb = rep(1, 3) )cpt( pal = "mpl_inferno", n = 100, colorRampPalette = FALSE, rev = FALSE, frgb = rep(1, 3) )
pal |
Character. Name or position of the colour gradient in the
cpt-city archive. Default |
n |
Integer. Number of colours to return when
|
colorRampPalette |
Logical. If |
rev |
Logical. If |
frgb |
Numeric vector of length 3. Per-channel scaling factors for
|
**'sf' / 'ggplot2' users:** use colorRampPalette = TRUE.
The sf plot method and ggplot2's
scale_*_gradientn need a *function*, not a character vector.
See the examples below.
A character vector of hex colours (length n) when
colorRampPalette = FALSE, or a colorRampPalette function
when colorRampPalette = TRUE.
library(cptcity) # Character vector — use with base graphics image(matrix(1:100), col = cpt(pal = "mpl_inferno")) image(matrix(1:100), col = cpt("idv_temperature", rev = TRUE)) # colorRampPalette function — use with sf and ggplot2 ## Not run: library(sf) nc <- st_read(system.file("shape/nc.shp", package = "sf")) plot(nc["AREA"], pal = cpt("mpl_inferno", colorRampPalette = TRUE)) library(ggplot2) ggplot(faithfuld, aes(waiting, eruptions)) + geom_raster(aes(fill = density)) + scale_fill_gradientn(colours = cpt(n = 100, pal = "mpl_inferno")) ## End(Not run) # Search for palettes by keyword find_cpt("temperature") image(matrix(1:100), col = cpt("idv_temperature")) # Tint the palette by scaling the red channel down image(matrix(1:100), col = cpt("mpl_inferno", frgb = c(0.3, 1, 1)))library(cptcity) # Character vector — use with base graphics image(matrix(1:100), col = cpt(pal = "mpl_inferno")) image(matrix(1:100), col = cpt("idv_temperature", rev = TRUE)) # colorRampPalette function — use with sf and ggplot2 ## Not run: library(sf) nc <- st_read(system.file("shape/nc.shp", package = "sf")) plot(nc["AREA"], pal = cpt("mpl_inferno", colorRampPalette = TRUE)) library(ggplot2) ggplot(faithfuld, aes(waiting, eruptions)) + geom_raster(aes(fill = density)) + scale_fill_gradientn(colours = cpt(n = 100, pal = "mpl_inferno")) ## End(Not run) # Search for palettes by keyword find_cpt("temperature") image(matrix(1:100), col = cpt("idv_temperature")) # Tint the palette by scaling the red channel down image(matrix(1:100), col = cpt("mpl_inferno", frgb = c(0.3, 1, 1)))
A character vector containing the names of every colour gradient
bundled in the cptcity package. Use find_cpt to
search this vector by keyword.
data(cpt_names)data(cpt_names)
A character vector of length 7140.
https://phillips.shef.ac.uk/pub/cpt-city/
data(cpt_names) head(cpt_names) length(cpt_names)data(cpt_names) head(cpt_names) length(cpt_names)
Incorporates 7140 colour gradients from the
cpt-city web archive
into R. Each palette is stored as a data.frame with columns
id, r, g, b.
cptReturn an interpolated colour palette or
colorRampPalette function.
find_cptSearch palette names by keyword.
luckyPick a random palette — "I'm Feeling Lucky" for colours.
show_cptDisplay multiple palettes side-by-side.
Maintainer: Sergio Ibarra-Espinosa [email protected] (ORCID)
Authors:
Sergio Ibarra-Espinosa [email protected] (ORCID)
Useful links:
Report bugs at https://github.com/ibarraespinosa/cptcity/issues/
Searches the 7140 cpt-city palette names and returns those that match your search term. Case-insensitive by default.
find_cpt(name, ignore.case = TRUE, fixed = FALSE)find_cpt(name, ignore.case = TRUE, fixed = FALSE)
name |
Character. Keyword to search for within palette names. |
ignore.case |
Logical. If |
fixed |
Logical. If |
A character vector of palette names that contain the keyword. Returns a zero-length vector if nothing matches.
This is a thin wrapper around grep. You can pass
regex patterns like "^oc_" to find all palettes starting with
"oc_" or "temperature|thermal" to match either term.
library(cptcity) # Case-insensitive search (default) find_cpt("temperature") # Case-sensitive find_cpt("Temperature", ignore.case = FALSE) # Regex: all palettes starting with "oc_" find_cpt("^oc_") # Literal match (faster, exact substring — case-sensitive) find_cpt("rain", fixed = TRUE) # Use the result directly image(matrix(1:100), col = cpt("idv_temperature")) ## Not run: library(ggplot2) ggplot(faithfuld, aes(waiting, eruptions)) + geom_raster(aes(fill = density)) + scale_fill_gradientn(colours = cpt(pal = "ncl_radar", n = 100)) ## End(Not run)library(cptcity) # Case-insensitive search (default) find_cpt("temperature") # Case-sensitive find_cpt("Temperature", ignore.case = FALSE) # Regex: all palettes starting with "oc_" find_cpt("^oc_") # Literal match (faster, exact substring — case-sensitive) find_cpt("rain", fixed = TRUE) # Use the result directly image(matrix(1:100), col = cpt("idv_temperature")) ## Not run: library(ggplot2) ggplot(faithfuld, aes(waiting, eruptions)) + geom_raster(aes(fill = density)) + scale_fill_gradientn(colours = cpt(pal = "ncl_radar", n = 100)) ## End(Not run)
Picks one of the 7140 colour gradients at random and returns it, optionally printing its name so you can re-use it later. Based on Google's "I'm Feeling Lucky" button.
lucky( n = 100, colorRampPalette = FALSE, rev = FALSE, message = TRUE, nseed, frgb = rep(1, 3) )lucky( n = 100, colorRampPalette = FALSE, rev = FALSE, message = TRUE, nseed, frgb = rep(1, 3) )
n |
Integer. Number of colours to return when
|
colorRampPalette |
Logical. If |
rev |
Logical. If |
message |
Logical. If |
nseed |
Integer. A seed value passed to |
frgb |
Numeric vector of length 3. Per-channel scaling factors for
|
A character vector of hex colours when
colorRampPalette = FALSE, or a colorRampPalette function
when colorRampPalette = TRUE.
library(cptcity) # Random palette each time you call it image(matrix(1:100), col = lucky()) image(matrix(1:100), col = lucky()) image(matrix(1:100), col = lucky(rev = TRUE)) # Reproducible random pick image(matrix(1:100), col = lucky(nseed = 42)) image(matrix(1:100), col = lucky(nseed = 42)) # same palette # Silent mode + sf-compatible output ## Not run: library(sf) nc <- st_read(system.file("shape/nc.shp", package = "sf")) plot(nc["AREA"], pal = lucky(colorRampPalette = TRUE, message = FALSE)) ## End(Not run)library(cptcity) # Random palette each time you call it image(matrix(1:100), col = lucky()) image(matrix(1:100), col = lucky()) image(matrix(1:100), col = lucky(rev = TRUE)) # Reproducible random pick image(matrix(1:100), col = lucky(nseed = 42)) image(matrix(1:100), col = lucky(nseed = 42)) # same palette # Silent mode + sf-compatible output ## Not run: library(sf) nc <- st_read(system.file("shape/nc.shp", package = "sf")) plot(nc["AREA"], pal = lucky(colorRampPalette = TRUE, message = FALSE)) ## End(Not run)
Draws each palette as a horizontal colour bar using
image, arranged in a grid that
automatically adapts to the number of palettes.
show_cpt(x, label = TRUE)show_cpt(x, label = TRUE)
x |
Character vector. Palette names (e.g. output from
|
label |
Logical. If |
Called for its side effect (a plot). Invisibly returns the palette names.
library(cptcity) # Search then visualise pals <- find_cpt("radar") show_cpt(pals) # Without labels show_cpt(pals, label = FALSE) # Single palette show_cpt("mpl_inferno")library(cptcity) # Search then visualise pals <- find_cpt("radar") show_cpt(pals) # Without labels show_cpt(pals, label = FALSE) # Single palette show_cpt("mpl_inferno")