[R] Recipe: Print a Color Sampler

Ana Nelson nelson.ana at gmail.com
Sun Oct 22 13:51:19 CEST 2006


I wrote this to help me choose a suitable plot color. You can set the  
value of pch to whichever type of point you are using in your plot,  
and then you can see what the various colors will look like. When  
this plot it stretched to A4/Letter landscape it prints quite nicely,  
even though it will look very bunched up initially. I have only used  
this on my own machine which runs Apple OSX 10.4.

I included a getColorName() convenience function to retrieve the name  
of the color from its position in the colors() array which is printed  
next to the dot in the plot.

printColorSampler <- function() {
     i <- 1
     pch <- 20
     l <- length(colors())
     k <- ceiling(sqrt(l))

     plot(floor(i/k), i %% k, pch=pch, col=colors()[i], xlim=c(0,  
k*2), ylim=c(0, k), axes=FALSE, xlab="", ylab="")

     for (i in 2:length(colors())) {
     	x <- floor(i/k)*2
     	y <- i %% k
     	col=colors()[i]
     	points(x, y, pch=pch, col=col)
     	text(x+0.5, y+0.2, i, col=col, cex=0.7)
     }
}

getColorName <- function(colorNumber) {
     colors()[colorNumber]
}



More information about the R-help mailing list