I propose to add a function that allows
to display colors selected by a text pattern
or by color vectors in a plot.
Wolfram Fischer
#--- showcolors.R
showcolors <-
function(
col = "red"
, index = NULL
, pie = TRUE
, lwd = 6
, cex = 1.0
, main = NULL
, sub = NULL
, ...
){
n.colors <- length( col )
if( n.colors > 1 ){
main <- deparse( substitute( col ) )
title <- qt.colors <- col
}else{
qx.colors <- grep( col, colors() )
if( ! is.null( index ) ) qx.colors = qx.colors[ index ]
else index = seq( along=qx.colors )
if( is.null( main ) ) main <- paste( sep="", col
, ' [', min(index), ':', max(index), ']' )
qt.colors <- colors()[ qx.colors ]
n.colors <- length( qt.colors )
}
if( pie ){
pie( x = rep( 1, n.colors ), col = qt.colors, labels = qt.colors
, main = main, sub = sub, cex = cex, ... )
}else{
plot( type='n', x = c( -3, 3 ), y = c( 1, n.colors )
, main = main, sub = sub, axes = FALSE, xlab = '', ylab = ''
)
segments(
x0 = rep( -0.5, n.colors ), y0 = 1 : n.colors
, x1 = rep( 0.5, n.colors ), y1 = 1 : n.colors
, col = qt.colors, lwd = lwd, ... )
text( x = rep( -0.55, n.colors ), y = 1 : n.colors
, labels= qt.colors, cex = cex, pos = 2 )
}
}
#--- showcolors.Rd
\name{showcolors}
\title{Displaying Colors}
\alias{showcolors}
\synopsis{
showcolors(col="red", \dots)
}
\description{
\code{showcolors} displays selected colors
(by searching color names containing a text pattern
or by a vector of colors)
in a pie or as bars.}
}
\usage{
showcolors(col="red", index=NULL, pie=TRUE, lwd=6, cex=1,
main=NULL, sub=NULL, \dots)
}
\arguments{
\item{col}{pattern string for search in \code{colors}.
Alternatively, a vector of colors.}
\item{index}{a numeric index for subselection
in the result of \code{col}.}
\item{pie}{display colors in a pie.
Alternatively: display colors as bars.}
\item{lwd}{line width for bars.}
\item{main}{an overall title for the plot.}
\item{sub}{a sub title for the plot.}
\item{\dots}{graphical parameters can be given as arguments
to \code{plot}.}
}
\details{
If \code{col} has length of 1 then all colors a displayed
with names containing the text string \code{col}.
The search is done by \code{grep}.
}
\seealso{
\code{\link{colors}},
\code{\link{palette}},
\code{\link{rainbow}},
\code{\link{grep}}.
}
\author{
Wolfram Fischer
}
\examples{
showcolors()
showcolors( palette() )
showcolors( rainbow(24) )
showcolors( "yellow" )
showcolors( "yellow", pie=FALSE )
showcolors( "blue", index=26:50 )
}
\keyword{color}
\keyword{dplot}
#---
On Fri, 24 Jan 2003 10:22:09 +0100, you wrote:>I propose to add a function that allows >to display colors selected by a text pattern >or by color vectors in a plot. >That's a nice function. I'd suggest some small changes: 1. Return the vector of colour names (invisibly), and have a plot TRUE argument to control whether the plot is drawn or not, i.e. reds <- showcolors("red", plot = FALSE) would be just a grep of the colour list. 2. Change the description of the pie = FALSE option to say it displays "lines", not "bars", or add a separate "lines" option. Otherwise it's not obvious that lty would do anything. Duncan Murdoch
Hi,
I also think this is a neat function. I might suggest adding a stop error
for dislexics like me. Something like
if(length(qx.colors)==0) stop(paste("No colors match query color
",col,".\n"))
Then there are no obscure error messages like
showcolors( col="gren",pie=FALSE)
Error in segments(x0, y0, x1, y1, col = col, lty = lty, lwd = lwd, ...) :
first argument invalid
In addition: Warning messages:
1: no finite arguments to min; returning Inf
2: no finite arguments to max; returning -Inf
Nicholas
On Fri, 24 Jan 2003 10:22:09 +0100, "Wolfram Fischer - Z/I/M"
<wolfram@fischer-zim.ch> said:> I propose to add a function that allows
> to display colors selected by a text pattern
> or by color vectors in a plot.
>
>>>>> "Wolfram" == Wolfram Fischer <- Z/I/M <wolfram@fischer-zim.ch>> >>>>> on Fri, 24 Jan 2003 10:22:09 +0100 writes:Wolfram> I propose to add a function that allows Wolfram> to display colors selected by a text pattern Wolfram> or by color vectors in a plot. Wolfram> Wolfram Fischer Wolfram> #--- showcolors.R I had a much simpler idea added to example(palettes) for R-devel (aka "pre-1.7.0") about 2 weeks ago: Maybe one could combine some of the underlying ideas... --- Martin Maechler <maechler@stat.math.ethz.ch> http://stat.ethz.ch/~maechler/ Seminar fuer Statistik, ETH-Zentrum LEO C16 Leonhardstr. 27 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1228 <>< ##------ Some palettes ------------ demo.pal <- function(n, border = if (n<32) "light gray" else NA, main = paste("color palettes; n=",n), ch.col = c("rainbow(n, start=.7, end=.1)", "heat.colors(n)", "terrain.colors(n)", "topo.colors(n)", "cm.colors(n)")) { nt <- length(ch.col) i <- 1:n; j <- n / nt; d <- j/6; dy <- 2*d plot(i,i+d, type="n", yaxt="n", ylab="", main=main) for (k in 1:nt) { rect(i-.5, (k-1)*j+ dy, i+.4, k*j, col = eval(parse(text=ch.col[k])), border = border) text(2*j, k * j +dy/4, ch.col[k]) } } n <- if(.Device == "postscript") 64 else 16 # Since for screen, larger n may give color allocation problem demo.pal(n) Wolfram> showcolors <- function( ......
Thanks for the inputs!
I have adjusted the function:
- it shows now a pie or lines and bars (default)
- it stops if there is no match for the searched color string
- it returns the list of the colors
Below you find the proposed new code.
Wolfram
#--- showcolors.R
showcolors <-
function(
col = "red" # pattern string for search in colors() or vector
of colors
, index = NULL # numeric vector for subselection
, pie = FALSE # display as pie, otherwise as bars and lines
, lwd = 1 # line width for lines
, cex = 1.0
, main = NULL
, sub = NULL
, plot = TRUE
, ...
){
n.colors <- length( col )
if( n.colors > 1 ){
main <- deparse( substitute( col ) )
title <- qt.colors <- col
}else{
qx.colors <- grep( col, colors() )
if( length( qx.colors ) == 0 )
stop( paste(sep=""
, "There is no color name which matches ``", col,
"''.\n" ) )
if( ! is.null( index ) ) qx.colors = qx.colors[ index ]
else index = seq( along=qx.colors )
if( is.null( main ) ) main <- paste( sep="", col
, ' [', min(index), ':', max(index), ']'
)
qt.colors <- colors()[ qx.colors ]
n.colors <- length( qt.colors )
}
if( plot ){
if( pie ){
pie( x = rep( 1, n.colors ), col = qt.colors, labels = qt.colors
, main = main, sub = sub, cex = cex, ... )
}else{
plot( type='n', x = c( -2, 2 ), y = c( 1, n.colors )
, main = main, sub = sub, axes = FALSE, xlab = '', ylab
= '' )
text( x = rep( 0, n.colors ), y = 1 : n.colors
, labels= qt.colors, cex = cex )
segments(
x0 = rep( -1.5, n.colors ), y0 = 1 : n.colors
, x1 = rep( -0.5, n.colors ), y1 = 1 : n.colors
, col = qt.colors, lwd = lwd, ... )
rect(
xleft = rep( 0.5, n.colors )
, ybottom = 1 : n.colors - 0.4
, xright = rep( 1.5, n.colors )
, ytop = 1 : n.colors + 0.4
, col = qt.colors, ... )
}
}
invisible( qt.colors )
}
#--- showcolors.Rd
\name{showcolors}
\title{Displaying Colors}
\alias{showcolors}
\synopsis{
showcolors(col="red", \dots)
}
\description{
\code{showcolors} displays selected colors
(by searching color names containing a text pattern
or given by a vector of colors)
as lines or bars or in a pie.
}
\usage{
showcolors(col="red", index=NULL, pie=FALSE, lwd=1, cex=1,
main=NULL, sub=NULL, plot=TRUE, \dots)
}
\arguments{
\item{col}{pattern string for search in \code{colors}.
Alternatively, a vector of colors.}
\item{index}{a numeric index for subselection
in the result of \code{col}.}
\item{pie}{logical. If \code{TRUE}, a colored pie is displayed.
Alternatively, colored lines and bars are displayed.}
\item{lwd}{line width for lines.}
\item{cex}{the character size to be used for the labels.}
\item{main}{an overall title for the plot.}
\item{sub}{a sub title for the plot.}
\item{plot}{logical. If \code{FALSE}, nothing is plotted.}
\item{\dots}{graphical parameters can be given as arguments
to \code{plot}.}
}
\details{
If \code{col} has length of 1 then all colors a displayed
with names containing the text string \code{col}.
The search is done by \code{grep}.
By default, a pie with the colors found is drawn,
labelled by the names of the colors.
}
\value{
A vector of the color names to show.
}
\seealso{
\code{\link{colors}},
\code{\link{palette}},
\code{\link{rainbow}},
\code{\link{grep}}.
}
\author{
Wolfram Fischer
}
\examples{
showcolors()
showcolors( palette() )
showcolors( rainbow(24) )
showcolors( "yellow" )
showcolors( "yellow", pie=TRUE )
showcolors( "blue", index=26:50 )
}
\keyword{color}
\keyword{dplot}
#---