R/choose_palette_br.R

Defines functions tanagr_palette

Documented in tanagr_palette

#' Chooses palette to use with base R.
#'
#' It generates a discrete palette with the number of colors available in the palette. If discrete = FALSE, a continuous palette is generated by linear interpolation (1:n) of the colors available in the palette.
#'
#' @param palette_name the name of the palette
#'
#' @param n number of colors requested (discrete). Number of colors for interpolation (continuous).
#'
#' @param discrete whether to use a discrete or continuous color palette (boolean)
#'
#' @examples
#' pal = tanagr_palette("bangsia_edwardsi")
#' data(iris)
#' plot(iris$Sepal.Width,
#'      iris$Sepal.Length,
#'      col = pal[as.numeric(iris$Species)],
#'      pch = 19,
#'      cex = 2)
#'
#' pal = tanagr_palette("dacnis_berlepschi", n = 20, discrete = FALSE)
#' data(volcano)
#' image(volcano,
#'       col = pal,
#'       main = "Colors: Dacnis berlepschi")
#' #Change length of interpolation vector
#' pal = tanagr_palette("dacnis_berlepschi", n = 100, discrete = FALSE)
#' image(volcano,
#'       col = pal,
#'       main = "Colors: Dacnis berlepschi")
#'
#' @export
#' @import grDevices

tanagr_palette <- function(palette_name, n, discrete = TRUE) {
    pal <- tanagr_palettes[[palette_name]]

    if (missing(n)) {
    	n <- length(pal)
  }
  	if (discrete == "TRUE" && n > length(pal)) {
    stop("Number of requested colors greater than number of discrete colors in palette. Please visualize palette using viz_palette(\"palette_name\") to check number of available colors in palette.")
  }
    if (discrete) {
    	return(pal[1:n])
  } else {
    	return(colorRampPalette(pal)(n))
  }
}
cdanielcadena/tanagR documentation built on Aug. 22, 2020, 3:23 a.m.