A function that displays the colors you can specify using the colors()
function
R has 657 built-in colors that can be accessed using the
colors()
function. Invoking
colors()
returns a 657 element character vector, where each element of the vector is a specific color name. The
colors()
function can als be used in conjunction with the
[ ]
subsetting operators to extract a specific subset of color names. For example,
colors()[4:10]
The following function generates a matrix of rectangles that displays the corresponding color of each of the 657 possible colors you can specify using the
colors()
function.
show.colors <- function(x=22,y=30){
par(mai=c(.4,.4,.4,.4), oma=c(.2,0,0,.2))
plot(c(-1,x),c(-1,y), xlab='', ylab='', type='n', xaxt='n', yaxt='n', bty='n')
for(i in 1:x){for(j in 1:y){
k <- y*(i-1)+j ; co <- colors()[k]
rect(i-1, j-1, i, j, col=co, border=1)}}
text(rep(-.5,y),(1:y)-.5, 1:y, cex=1.2-.016*y)
text((1:x)-.5, rep(-.5,x), y*(0:(x-1)), cex=1.2-.022*x)
title('col=colors()[n]')
}
show.colors()