R/reorder.R

Defines functions scale_y_reordered scale_x_reordered reorder_within

# No license on code: note this same code has been reused in the tidytext
# package  (excluded here to reduce dependencies/size).
reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
  new_x <- paste(x, within, sep = sep)
  stats::reorder(new_x, by, FUN = fun)
}

scale_x_reordered <- function(..., sep = "___") {
  reg <- paste0(sep, ".+$")
  ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
}

scale_y_reordered <- function(..., sep = "___") {
  reg <- paste0(sep, ".+$")
  ggplot2::scale_y_discrete(labels = function(x) gsub(reg, "", x), ...)
}
britishredcrosssociety/nhs-capacity documentation built on June 23, 2022, 8:14 p.m.