Learn R Programming

oce (version 0.9-14)

colormap: Calculate color map

Description

Map values to colors, for use in palettes and plots.

Usage

colormap(z,
         zlim, zclip=FALSE,
         breaks, col=oceColorsJet,
         name, x0, x1, col0, col1, blend=0,
         missingColor,
         debug=getOption("oceDebug"))

Arguments

z
an optional vector or other set of numerical values to be examined. If z is given, the return value will contain an item named zcol that will be a vector of the same length as z, containing a color
zlim
optional vector containing two numbers that specify the z limits for the colorscale. If not given, this will be determined from the other arguments, as follows. If name is given, then the
zclip
logical, indicating whether to clip the colors to those corresponding to zlim, if the latter is provided. Clipped regions will be colored with missingColor.
breaks
an optional indication of break points between color levels (see image). If this is provided, the arguments name through blend are all ignored (see Details
col
either a vector of colors or a function taking a numerical value as its single argument and returning a vector of colors. The value of col is ignored if name is provided, or if x0 through col
name
an optional string naming a built-in colormap (one of "gmt_relief", "gmt_ocean", "gmt_globe" or "gmt_gebco") or the name of a file or URL that contains a color map specification in GMT
x0, x1, col0, col1
Vectors that specify a color map. They must all be the same length, with x0 and x1 being numerical values, and col0 and col1 being colors. The colors may be strings (e.g. "red"<
blend
a number indicating how to blend colors within each band. This is ignored except when x0 through col1 are supplied. A value of 0 means to use col0[i] through the interval x0[i] to
missingColor
color to use for missing values. If not provided, this will be "gray", unless name is given, in which case it comes from that color table.
debug
a flag that turns on debugging. Set to 1 to get a moderate amount of debugging information, or to 2 to get more.

Details

This is a multi-purpose function that generally links (``maps'') numerical values to colors. The return value can specify colors for points on a graph, or breaks and col vectors that are suitable for use by drawPalette, imagep or image. There are three ways of specifying color schemes, and colormap works by checking for each condition in turn.
  • Case A.Supplyzbut nothing else. In this case,breakswill be set topretty(z, 10)and things are otherwise as in case 2.
  • Case B.Supplybreaks. In this case,breaksandcolare used together to specify a color scheme. Ifcolis a function, then it is expected to take a single numerical argument that specifies the number of colors, and this number will be set tolength(breaks)-1. Otherwise,colmay be a vector of colors, and its length must be one less than the number of breaks. (NB. ifbreaksis given, then all other arguments exceptcolandmissingColorare ignored.)html{The figure below explains the (breaks,col) method of specifying a color mapping. Note that there must be one more break than color. This is the method used by e.g.image.
    colormap_fig_1.png
    {options: width=400px alt="Figure: colormap\_fig\_1.png"}
Case C. Do not supply breaks, but supply name instead. This name may be the name of a pre-defined color palette ("gmt_relief", "gmt_ocean", "gmt_globe" or "gmt_gebco"), or it may be the name of a file (including a URL) containing a color map in the GMT format (see References). (NB. if name is given, then all other arguments except z and missingColor are ignored.) Case D. Do not supply either breaks or name, but instead supply each of x0, x1, col0, and col1. These values are specify a value-color mapping that is similar to that used for GMT color maps. The method works by using seq to interpolate between the elements of the x0 vector. The same is done for x1. Similarly, colorRampPalette is used to interpolate between the colors in the col0 vector, and the same is done for col1. html{The figure below explains the (x0, x1, col0, col1) method of specifying a color mapping. Note that the each of the items has the same length. The case of blend=0, which has color col0[i] between x0[i] and x1[i], is illustrated below.

colormap_fig_2.png
{options: width=400px alt="Figure: colormap_fig_2.png"}}

A list containing the following (not necessarily in this order)
  • zcol, a vector of colors forz, ifzwas provided, otherwise"black"
  • zlim, a two-element vector suitable as the argument of the same name supplied toimageorimagep
  • breaksandcol, vectors of breakpoints and colors, suitable as the same-named arguments toimageorimagep
  • x0andx1, numerical vectors of the sides of color intervals, andcol0andcol1, vectors of corresponding colors. The meaning is the same as on input. The purpose of returning these four vectors is to permit users to alter color mapping, as in example 3 inExamples.
  • missingColor, a color that could be used to specify missing values, e.g. as the same-named argument toimagep. If this is supplied as an argument, its value is repeated in the return value. Otherwise, its value is either"gray"or, in the case ofnamebeing given, the value in the GMT color map specification.

library(oce)

## Example 1. color scheme for points on xy plot x <- seq(0, 1, length.out=40) y <- sin(2 * pi * x) par(mar=c(3, 3, 1, 1)) mar <- par('mar') # prevent margin creep by drawPalette() ## First, default breaks c <- colormap(y) drawPalette(c$zlim, col=c$col, breaks=c$breaks) plot(x, y, bg=c$zcol, pch=21, cex=1) grid() par(mar=mar) ## Second, 100 breaks, yielding a smoother palette c <- colormap(y, breaks=100) drawPalette(c$zlim, col=c$col, breaks=c$breaks) plot(x, y, bg=c$zcol, pch=21, cex=1) grid() par(mar=mar)

## Example 2. topographic image with a standard color scheme par(mfrow=c(1,1)) data(topoWorld) cm <- colormap(name="gmt_globe") imagep(topoWorld, breaks=cm$breaks, col=cm$col) ## visualize color map # plot(seq_along(cm$x0), cm$x0, pch=21, bg=cm$col0) # grid() # points(seq_along(cm$x1), cm$x1, pch=21, bg=cm$col1)

## Example 3. topographic image with modified colors cm <- colormap(name="gmt_globe") deep <- cm$x0 < -4000 cm$col0[deep] <- 'black' cm$col1[deep] <- 'black' cm <- colormap(x0=cm$x0, x1=cm$x1, col0=cm$col0, col1=cm$col1) imagep(topoWorld, breaks=cm$breaks, col=cm$col)

## Example 4. image of world topography with water colorized ## smoothly from violet at 8km depth to blue ## at 4km depth, then blending in 0.5km increments ## to white at the coast, with tan for land. cm <- colormap(x0=c(-8000, -4000, 0, 100), x1=c(-8000, -4000, 0, 100), col0=c("violet","blue","white","tan"), col1=c("violet","blue","white","tan"), blend=c(100, 8, 0)) lon <- topoWorld[['longitude']] lat <- topoWorld[['latitude']] z <- topoWorld[['z']] imagep(lon, lat, z, breaks=cm$breaks, col=cm$col) contour(lon, lat, z, levels=0, add=TRUE)

Information on GMT software is given at http://gmt.soest.hawaii.edu. Diagrams showing the GMT color schemes are at http://www.geos.ed.ac.uk/it/howto/GMT/CPT/palettes.html, and numerical specifications for some color maps are at http://www.beamreach.org/maps/gmt/share/cpt, http://soliton.vm.bytemark.co.uk/pub/cpt-city, and other sources.

[object Object]

misc