| 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/>, including dedicated NOAA ramps for weather, ocean, and climate work. 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 [aut, cre] (ORCID: <https://orcid.org/0000-0002-3162-1905>) |
| Maintainer: | Sergio Ibarra-Espinosa <[email protected]> |
| License: | GPL-3 |
| Version: | 2.4.2 |
| Built: | 2026-10-09 12:08:20 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 colour gradients from
https://phillips.shef.ac.uk/pub/cpt-city/, including dedicated
NOAA ramps ("noaa", "noaa_nws", "noaa_nhc",
"noaa_nexrad", "noaa_goes", "noaa_buoy",
"noaa_ncei", "noaa_nmfs", "noaa_jetstream",
"noaa_tornado", "noaa_wind_chill",
"noaa_heat_index", "noaa_coastal").
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.
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 colour gradients from the
cpt-city web archive
into R, including dedicated NOAA ramps. 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 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 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")