R/scales-ust.R

Defines functions scale_fill_ust scale_color_ust

Documented in scale_color_ust scale_fill_ust

#' Color scale constructor for ust colors
#'
#' @param palette Character name of palette in ust_palettes
#' @param reverse Boolean indicating whether the palette should be reversed
#' @param ... Additional arguments passed to discrete_scale() or
#'            scale_color_gradientn(), used respectively when discrete is TRUE or FALSE
#'
scale_color_ust <- function(palette = "main", discrete = TRUE, reverse = FALSE, ...) {

  if (discrete) {

    if (reverse) { pal <- function() { scales::manual_pal(unname(rev(ust_palettes[[palette]]))) }
    } else {
    pal <- function() { scales::manual_pal(unname(ust_palettes[[palette]])) }
    }

    ggplot2::discrete_scale("color", paste0("ust_", palette), palette = pal(), ...)
  } else {
    pal <- ust_pal(palette = palette, reverse = reverse)
    ggplot2::scale_color_gradientn(colors = pal(256), ...)
  }
}

#' Fill scale constructor for ust colors
#'
#' @param palette Character name of palette in ust_palettes
#' @param reverse Boolean indicating whether the palette should be reversed
#' @param ... Additional arguments passed to discrete_scale() or
#'            scale_fill_gradientn(), used respectively when discrete is TRUE or FALSE
#'
scale_fill_ust <- function(palette = "main", discrete = TRUE, reverse = FALSE, ...) {

  if (discrete) {

    if (reverse) { pal <- function() { scales::manual_pal(unname(rev(ust_palettes[[palette]]))) }
    } else {
      pal <- function() { scales::manual_pal(unname(ust_palettes[[palette]])) }
    }

    ggplot2::discrete_scale("fill", paste0("ust_", palette), palette = pal(), ...)
  } else {
    pal <- ust_pal(palette = palette, reverse = reverse)

    ggplot2::scale_fill_gradientn(colors = pal(256), ...)
  }
}
UniversityOfSaintThomas/ustr documentation built on April 23, 2020, 12:44 a.m.