R/plot_crayons.R

Defines functions plot_crayons

Documented in plot_crayons

#' Illustration of crayon colors
#'
#' Creates a plot of the crayon colors in \code{\link{brocolors}}
#'
#' @param method2order method to order colors (\code{"hsv"} or \code{"cluster"})
#' @param cex character expansion for the text
#' @param mar margin paramaters; vector of length 4 (see \code{\link[graphics]{par}})
#'
#' @return None
#'
#' @author Karl W Broman, \email{kbroman@@biostat.wisc.edu}
#' @references \url{http://en.wikipedia.org/wiki/List_of_Crayola_crayon_colors}
#' @seealso \code{\link{brocolors}}
#' @keywords hplot
#'
#' @examples
#' plot_crayons()
#'
#' @export
#' @importFrom grDevices rgb2hsv col2rgb
#' @importFrom graphics par plot rect text
#' @importFrom stats dist hclust
#'
plot_crayons <-
function(method2order=c("hsv", "cluster"), cex=0.6, mar=rep(0.1, 4))
{
  method2order <- match.arg(method2order)

  crayons <- brocolors("crayons")

  # get rgb
  colval <- col2rgb(crayons)

  if(method2order == "hsv") {
    # convert to hsv
    colval <- t(rgb2hsv(colval))

    # order the colors; first two lines are to get black/gray/silver/white first
    ord <- order(names(crayons)!="Black", names(crayons)!="Gray",
                 names(crayons)!="Silver", names(crayons)!="White",
                 colval[,1], colval[,2], colval[,3])

  } else {
    ord <- hclust(dist(t(colval)))$ord
  }

  oldmar <- par("mar")
  on.exit(par(mar=oldmar))

  par(mar=mar)
  x <- (1:7)-1
  y <- (1:19)-1
  x <- rep(x, each=19)
  y <- rep(y, 7)

  plot(0, 0, type="n", xlab="", ylab="", xaxs="i", yaxs="i",
       xlim=c(0, max(x)+1), ylim=c(max(y)+0.5, -0.5),
       xaxt="n", yaxt="n")

  dx <- 0.2
  dy <- 0.4
  rect(x+dx/4, y-dy, x+dx, y+dy, border="black",
       col=crayons[ord])

  text(x+dx*1.2, y, names(crayons)[ord], cex=cex, adj=c(0, 0.5))
}
AlfonsoRReyes/missingR documentation built on May 5, 2019, 4:54 a.m.