R/scale_norm.R

Defines functions my_norm scale_norm

Documented in scale_norm

#' Performs transformation on continuous variables.
#'
#' Performs transformation on continuous variables for the heatmap color scales.
#'
#' @param x Numeric vector.
#' @inheritParams draw_heat
#' @return Numeric vector of the transformed `x`.
#' @export
#' @examples scale_norm(1:5)
#' scale_norm(1:5, "normalize")
#'
scale_norm <- function(x, trans_type = c("percentize", "normalize", "scale", "none")) {
  trans_type <- match.arg(trans_type)

  switch(trans_type,
    percentize = stats::ecdf(x)(x),
    scale = as.numeric(scale(x)),
    normalize = my_norm(x),
    none = x
  )
}

my_norm <- function(x) {
  x <- x - min(x, na.rm = T)
  x <- x / max(x, na.rm = T)
  x
}
trang1618/treeheatr documentation built on July 30, 2023, 1:20 a.m.