R/coord-scaffold.r

Defines functions coord_scaffold

Documented in coord_scaffold

#' @title Convenience coordinate system for scaffolding axes
#'
#' @description 2- (and 3-) dimensional biplots require that coordinates lie on
#'   the same scale but may additionally benefit from a square plotting window.
#'   While `CoordRect` provides control of coordinate and window aspect ratios,
#'   the convenience `CoordScaffold` system also fixes the coordinate aspect
#'   ratio at `1` and gives the user control only of the plotting window.
#'
#' @inheritParams ggplot2::coord_fixed
#' @param window_ratio aspect ratio of plotting window
#' @example inst/examples/ex-coord-scaffold.r
#' @export
coord_scaffold <- function(
    window_ratio = 1,
    xlim = NULL, ylim = NULL, expand = TRUE, clip = "on"
) {
  ggplot2:::check_coord_limits(xlim)
  ggplot2:::check_coord_limits(ylim)
  ggproto(
    NULL, CoordScaffold,
    limits = list(x = xlim, y = ylim),
    window_ratio = window_ratio,
    expand = expand,
    clip = clip
  )
}

#' @rdname ordr-ggproto
#' @format NULL
#' @usage NULL
#' @export
CoordScaffold <- ggproto(
  "CoordScaffold", CoordRect,
  
  # require coordinate aspect ratio to be 1
  aspect = function(self, ranges) {
    diff(ranges$y.range) / diff(ranges$x.range)
  }
)
corybrunson/ordr documentation built on Feb. 24, 2025, 6:34 a.m.