[R] Recipe: Print a Color Sampler

Ana Nelson nelson.ana at gmail.com
Sun Oct 22 17:44:39 CEST 2006


Thanks, guys, for all the mods! :-)

Here is another version to allow people to display the dots if they  
wish and also to set a different background color. I think this is  
useful just for identifying the colors, but also to see what a  
particular shape/color/background color combination will look like.

I'll post this on the wiki in a day or two unless somebody else gets  
to it first. (Thanks, Antonio, I didn't know about it.)


getColorName <- function(colorNumber) colors()[colorNumber]
printColorSampler <- function(n = 0, pch = "n", bg = "white", cex=1) {
    i <- seq(colors())
    k <- ceiling(sqrt(length(i)))
    xy <- cbind(floor(i/k)*2, i %% k)
    type <- switch(as.character(pch), "n"="n", "p")
    par(bg=bg, cex=cex)
    plot(xy, type = type, axes = FALSE, xlab = "", ylab = "",  
pch=pch, col=colors())
    par(cex=1) # Reset cex so that it doesn't interfere with text  
size cex.
    text(xy[,1]+.5, xy[,2]+.2, i, col = colors(), cex = 0.7)
    if (n > 0)
       colors()[identify(xy, n = n, labels = colors(), plot = FALSE)]
}
# test
printColorSampler(0)
printColorSampler(1)
printColorSampler(pch=20, bg="black")


On 22 Oct 2006, at 15:28, Antonio, Fabio Di Narzo wrote:

> Really fine.
> Should this go on the wiki?
>
> Antonio.
>
> 2006/10/22, Gabor Grothendieck <ggrothendieck at gmail.com>:
>> Here it is again corrected:
>>
>> getColorName <- function(colorNumber) colors()[colorNumber]
>> printColorSampler <- function(n = 0) {
>>    i <- seq(colors())
>>    k <- ceiling(sqrt(length(i)))
>>    xy <- cbind(floor(i/k)*2, i %% k)
>>    plot(xy, type = "n", axes = FALSE, xlab = "", ylab = "")
>>    text(xy[,1]+.5, xy[,2]+.2, i, col = colors(), cex = 0.7)
>>    if (n > 0)
>>       colors()[identify(xy, n = n, labels = colors(), plot = FALSE)]
>> }
>> # test
>> printColorSampler(0)
>> printColorSampler(1)
>>
>>
>> On 10/22/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
>>> I have removed the dots, vectorized it and changed the
>>> argument to the number points to be identified (default 0):
>>>
>>> getColorName <- function(colorNumber) colors()[colorNumber]
>>> printColorSampler <- function(n = 0) {
>>>   i <- seq(colors())
>>>   k <- ceiling(sqrt(length(i)))
>>>   xy <- cbind(floor(i/k)*2, i %% k)
>>>   plot(xy, type = "n", axes = FALSE, xlab = "", ylab = "")
>>>   text(x+.5, y+.2, i, col = colors(), cex = 0.7)
>>>   if (n > 0)
>>>      colors()[identify(xy, n = n, labels = colors(), plot = FALSE)]
>>> }
>>> # test
>>> printColorSampler(0)
>>> printColorSampler(1)
>>>
>>>
>>>
>>> On 10/22/06, Stefano Calza <calza at med.unibs.it> wrote:
>>>> 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 at 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.
>>>>
>>>>
>>>> ______________________________________________
>>>> R-help at stat.math.ethz.ch mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide http://www.R-project.org/ 
>>>> posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>>
>>>>
>>>>
>>>
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting- 
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting- 
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list