[R] Recipe: Print a Color Sampler

Stefano Calza calza at med.unibs.it
Sun Oct 22 14:28:51 CEST 2006


Thanks, might be very useful.

I attached a slightly modified version that uses identify (whith an argument to choose if use it), to return the name of the colors on the panel. Left-click on the choosen colors, and right click to end.

Stefano


On Sun, Oct 22, 2006 at 12:51:19PM +0100, Ana Nelson wrote:
<Ana>I wrote this to help me choose a suitable plot color. You can set the  
<Ana>value of pch to whichever type of point you are using in your plot,  
<Ana>and then you can see what the various colors will look like. When  
<Ana>this plot it stretched to A4/Letter landscape it prints quite nicely,  
<Ana>even though it will look very bunched up initially. I have only used  
<Ana>this on my own machine which runs Apple OSX 10.4.
<Ana>
<Ana>I included a getColorName() convenience function to retrieve the name  
<Ana>of the color from its position in the colors() array which is printed  
<Ana>next to the dot in the plot.
<Ana>
<Ana>printColorSampler <- function() {
<Ana>     i <- 1
<Ana>     pch <- 20
<Ana>     l <- length(colors())
<Ana>     k <- ceiling(sqrt(l))
<Ana>
<Ana>     plot(floor(i/k), i %% k, pch=pch, col=colors()[i], xlim=c(0,  
<Ana>k*2), ylim=c(0, k), axes=FALSE, xlab="", ylab="")
<Ana>
<Ana>     for (i in 2:length(colors())) {
<Ana>     	x <- floor(i/k)*2
<Ana>     	y <- i %% k
<Ana>     	col=colors()[i]
<Ana>     	points(x, y, pch=pch, col=col)
<Ana>     	text(x+0.5, y+0.2, i, col=col, cex=0.7)
<Ana>     }
<Ana>}
<Ana>
<Ana>getColorName <- function(colorNumber) {
<Ana>     colors()[colorNumber]
<Ana>}
<Ana>
<Ana>______________________________________________
<Ana>R-help a stat.math.ethz.ch mailing list
<Ana>https://stat.ethz.ch/mailman/listinfo/r-help
<Ana>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
<Ana>and provide commented, minimal, self-contained, reproducible code.
-------------- parte successiva --------------
printColorSampler <- function(identify=TRUE) {
  i <- 1
  pch <- 20
  l <- length(colors())
  k <- ceiling(sqrt(l))

  rec.coord <- data.frame(x=rep(NA,l),y=rep(NA,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="")
  rec.coord[i,] <- c(floor(i/k),i %% k)
    
  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)
    rec.coord[i,] <- c(x,y)
  }
  if(identify)
    {
      wCol <- identify(rec.coord,labels=colors(),plot=FALSE)
      return(colors()[wCol])
    }
}

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



More information about the R-help mailing list