Re: [R] Recipe: Print a Color Sampler

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sun 22 Oct 2006 - 16:11:05 GMT

Plotting points with a pch = NA also plots nopoints so we could remove the type= and the switch statement. Also it needs an on.exit to reset par back to what it was. The cex seems to have no effect so I removed it.

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

# pch = NA means no points plotted. pch = 20 plots small dots. # n is the number of points to identify interactively with mouse printColorSampler <- function(n = 0, pch = NA, bg = "white") {

   i <- seq(colors())
   k <- ceiling(sqrt(length(i)))
   xy <- cbind(floor(i/k)*2, i %% k)
   opar <- par(bg = bg)
   on.exit(par = opar)
   plot(xy, axes = FALSE, xlab = "", ylab = "", pch=pch, col=colors())    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 10/22/06, Ana Nelson <nelson.ana@gmail.com> wrote:
> 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@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@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@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@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@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@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@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@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@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. Received on Mon Oct 23 04:48:37 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 22 Oct 2006 - 19:30:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.