R: Color Names

colors {grDevices} | R Documentation

## Color Names

### Description

Returns the built-in color names which R knows about.

### Usage

    colors()
    colours()

### Details

These color names can be used with a `col=` specification in graphics functions.

An even wider variety of colors can be created with primitives `rgb` and `hsv` or the derived `rainbow`, `heat.colors`, etc.

### Value

A character vector containing all the built-in color names.

### See Also

`palette` for setting the ‘palette’ of colors for `par(col=`<num>`)`; `rgb`, `hsv`, `hcl`, `gray`; `rainbow` for a nice example; and `heat.colors`, `topo.colors` for images.

`col2rgb` for translating to RGB numbers and extended examples.

### Examples

    cl <- colors()
    length(cl); cl[1:20]

    ### ----------- Show (almost) all named colors ---------------------

    ## 1) with traditional 'graphics' package:
    showCols1 <- function(bg = "gray", cex = 0.75, srt = 30) {
        m <- ceiling(sqrt(n <- length(cl <- colors())))
        length(cl) <- m*m; cm <- matrix(cl, m)
        ##
        require("graphics")
        op <- par(mar=rep(0,4), ann=FALSE, bg = bg); on.exit(par(op))
        plot(1:m,1:m, type="n", axes=FALSE)
        text(col(cm), rev(row(cm)), cm,  col = cl, cex=cex, srt=srt)
    }
    showCols1()

    ## 2) with 'grid' package:
    showCols2 <- function(bg = "grey", cex = 0.75, rot = 30) {
        m <- ceiling(sqrt(n <- length(cl <- colors())))
        length(cl) <- m*m; cm <- matrix(cl, m)
        ##
        require("grid")
        grid.newpage(); vp <- viewport(w = .92, h = .92)
        grid.rect(gp=gpar(fill=bg))
        grid.text(cm, x = col(cm)/m, y = rev(row(cm))/m, rot = rot,
                  vp=vp, gp=gpar(cex = cex, col = cm))
    }
    showCols2()
    showCols2(bg = "gray33")

[Package grDevices version 2.15.1 Index]