R/size_order.R

Defines functions size_order

Documented in size_order

#' Make an ordered factor with most important level first
#'
#' Useful when making bar plots and have the most common level in the bottom.
#' Also possible to remove uncommon levels and create new level called other.
#' @param x Vector
#' @param other_count How many levels should be considered as other level
#' @param other_level What the new level should be called
#' @examples
#' table(ggplot2::diamonds$cut)
#' in_size_order <- size_order(ggplot2::diamonds$cut)
#' table(in_size_order)
#' in_size_order <- size_order(ggplot2::diamonds$cut, 2, "Others")
#' table(in_size_order)
#' @export
size_order <- function(x, other_count = NULL, other_level = "\u00D6vriga") {
  ordning <- names(sort(table(x), decreasing = TRUE))
  x <- ordered(x, levels = ordning)

  if (!is.null(other_count)) {
    n_levels <- length(levels(x)) - other_count
    levels(x) <- c(levels(x)[1:n_levels], rep(other_level, other_count))
  }
  x
}
swehip/shprplotfun documentation built on Oct. 21, 2022, 8:26 a.m.