The colors() function
The R colors() and colours() functions return a character vector
containing the named colors that R knows about.
> colors() [1] "white" "aliceblue" "antiquewhite" [4] "antiquewhite1" "antiquewhite2" "antiquewhite3" [7] "antiquewhite4" "aquamarine" "aquamarine1" ... [649] "wheat3" "wheat4" "whitesmoke" [652] "yellow" "yellow1" "yellow2" [655] "yellow3" "yellow4" "yellowgreen"
Subsetting colors using the grep() function
Here's how to use the grep() function to identify the greens in the character
vector producted by the colors() function:
colors()[ grep( "green", colors() ) ]
The output of this command is:
> colors()[ grep( "green", colors() ) ] [1] "darkgreen" "darkolivegreen" "darkolivegreen1" [4] "darkolivegreen2" "darkolivegreen3" "darkolivegreen4" [7] "darkseagreen" "darkseagreen1" "darkseagreen2" [10] "darkseagreen3" "darkseagreen4" "forestgreen" [13] "green" "green1" "green2" [16] "green3" "green4" "greenyellow" [19] "lawngreen" "lightgreen" "lightseagreen" [22] "limegreen" "mediumseagreen" "mediumspringgreen" [25] "palegreen" "palegreen1" "palegreen2" [28] "palegreen3" "palegreen4" "seagreen" [31] "seagreen1" "seagreen2" "seagreen3" [34] "seagreen4" "springgreen" "springgreen1" [37] "springgreen2" "springgreen3" "springgreen4" [40] "yellowgreen"
A function that plots the output of colors()
The function plotNamedColors(), presented below, plots the named colors
and their indices and optionally creates a PNG file of the plot. The code is an exercise
in displaying the actual colors and their indices using the following graphics functions:
par()plot()rect()text()dev.copy()dev.off()
## plotNamedColors.r
## 09-Feb-2006
##
## Conrad Halling
## conrad.halling@sphaerula.com
##
## Plot the named colors available from the colors() function.
##
## The plot adjusts its height and width depending on the number of colors to
## plot and the colCount parameter. The secret to getting this to work is to
## use the "asp" parameter.
##
## Parameters of function plotNamedColors():
##
## pattern grep pattern for selecting colors (e.g., "green", "gray")
##
## colCount number of columns of color cells in the plot
##
## border color of border around color cells
##
## makePng make a PNG file of the plot; if no pattern parameter is
## provided, the file name is "colors.png"; if a pattern
## parameter is provided, the file name is the pattern
## parameter pasted to ".png" (e.g., "green.png")
plotNamedColors <-
function(
pattern = "",
colCount = 40,
border = "lightgray",
makePng = FALSE )
{
## Set up the title of the plot.
if ( pattern != "" )
{
main <-
paste(
"colors()[ grep( \"",
pattern,
"\", colors() ]",
sep = "" )
}
else
{
main <- "colors()"
}
## Determine the height and width of the plot.
## The values used for calculations are the widths of plots in mm on my
## PowerBook.
colrs <- colors()[ grep( pattern, colors() ) ]
rowCount <- ceiling( length( colrs ) / colCount )
height <-
( ( 183 - 170 ) / 183 + ( ( rowCount / 17 ) * 170 ) / 183 ) * 10.0
width <-
( ( 144 - 137 ) / 144 + ( ( colCount / 40 ) * 137 ) / 144 ) * 8.0
## Create a new plotting window.
get( getOption( "device" ) )( width = width, height = height )
## Set the plotting parameters that can't be included in the
## call to plot. This sets up narrow margins.
par( mar = c( 1, 1, 2, 1 ) + 0.1 )
## This plot command sets up the coordinates, hides the points,
## hides the axes, hides the axis labels, sets the title, and fixes
## the plot aspect.
plot(
x = c( 1, colCount ),
y = c( 0, rowCount ),
type = "n",
xaxt = "n",
yaxt = "n",
xlab = "",
ylab = "",
main = main,
asp = 3.0 )
## For each row of color cells...
for ( i in 1:rowCount ) {
## For each column, a color cell, ...
for ( j in 1:colCount ) {
## Get the index of the color
colorIndex <- ( i - 1 ) * colCount + j
## Don't draw cells for nonexistent colors in the bottom row.
if ( colorIndex <= length( colrs ) ) {
## Draw a rect filled with the color.
rect(
xleft = j - 0.5,
ybottom = rowCount - i + 0.44,
xright = j + 0.5,
ytop = rowCount - i + 1,
border = border,
col = colrs[ colorIndex ] )
## Draw the index every fifth rect.
if ( 0 == colorIndex %% 5 ) {
text(
x = j,
y = rowCount - i + 0.2,
labels = as.character( colorIndex ),
offset = 0,
cex = 1.0 )
}
}
}
}
## If requested, create the PNG output file.
## This requires that the X11 interface be active.
if ( makePng && capabilities( what = "png" ) ) {
## Create the file name.
if ( pattern != "" ) {
fileName <- paste( pattern, ".png", sep = "" )
}
else {
fileName <- "colors.png"
}
## Copy the plot to the PNG device.
## Multiply height and width by 72 to get the PNG dimensions.
dev.copy(
device = png,
filename = fileName,
width = width * 72,
height = height * 72 )
dev.off()
}
}
Here's how to use this function:
## Source the file containing the function: source( file = "http://sphaerula.com/R/plotNamedColors.r" ) ## Plot all named colors: plotNamedColors() ## Plot greens: plotNamedColors( pattern = "green" ) ## Plot grays and greys; note differences: plotNamedColors( pattern = "gray" ) plotNamedColors( pattern = "grey" )
The PNG output produced by the command:
plotNamedColors( makePng = TRUE )
is:
Color map of the colors() function
The PNG output produced by the command:
plotNamedColors( pattern = "green", makePng = TRUE )
is:
Color map of the green colors from the colors() function
Here's an example of using a color by its index:
## Plot points using the color at index 27 of the vector produced
## by colors(), then plot points using the color at index 14 of
## the vector produced by colors()[ grep( "green", colors() ) ]:
get( getOption( "device" ) )()
d.f1 <- data.frame( x = rnorm( 10 ), y = rnorm( 10 ) )
d.f2 <- data.frame( x = rnorm( 10 ), y = rnorm( 10 ) )
plot( y ~ x, data = d.f1, type = "n",
xlim = c( -3, 3 ), ylim = c( -3, 3 ) )
## Plot the first set of points in a blue color.
points( y ~ x, data = d.f1, col = colors()[ 27 ] )
## Plot the second set of points in a green color.
greens <- colors()[ grep( "green", colors() ) ]
points( y ~ x, data = d.f2, col = greens[ 14 ] )
Other resources
epitools package
The epitools package provides two functions, colors.plot() and
colors.matrix(), for viewing and selecting R’s named colors.
## Load the epitools package: library( package = epitools ) ## View the color matrix: colors.plot() ## Plot using the color at matrix coordinate [ 15, 5 ]: get( getOption( "device" ) )() d.f <- data.frame( x = rnorm( 10 ), y = rnorm( 10 ) ) plot( y ~ x, data = d.f, type = "n" ) points( y ~ x, data = d.f, col = colors.matrix()[ 15, 5 ] )
Earl F. Glynn’s R color chart
Earl F. Glynn provides a color chart of R’s named colors in both HTML and PDF formats. Glynn’s presentation is comprehensive and elegant.
R graph gallery
The R Graph Gallery presents a page of the R colors in RGB space by Martin Maechler.