axis_limits: Manage axis limits

View source: R/axis_limits.R

axis_limitsR Documentation

Manage axis limits

Description

Manage axis limits. Enforce certain points to be included (e.g. 0), and/or force axis limits to be the same between x- and y-axes.

Usage

axis_limits(p, expand_limits = NULL, axis_limits_equal = FALSE)

Arguments

p

object of class 'ggplot'. Limits are adjusted for this plot.

expand_limits

list. If not NULL, then it is (effectively) passed onto ggplot2::expand_limits to ensure that certain values are included in the plot (such as, for example, 0 if that is the minimum value possible but it may not be plotted). If not named, then must consist of one numeric vector that will then force all values in the numeric value to be included in the plot. If named, then must have names x and/or y, with the elements again being numeric vectors that must be included in plot.

axis_limits_equal

logical. If TRUE, then the ranges on the x- and y-axes must be equal. Effectively applied after expand_grid is applied. Default is FALSE.

Examples

data('cars', package = 'datasets')
library(ggplot2)
p <- ggplot(cars, aes(speed, dist)) +
  geom_point()

axis_limits(
  p,
  axis_limits_equal = TRUE)

# both axes
axis_limits(
  p,
  expand_limits = list(200))
# x only
axis_limits(
  p,
  expand_limits = list(x = 75))
# y only
axis_limits(
  p,
  expand_limits = list(y = 200))
# lower and upper expansion
axis_limits(
  p,
  expand_limits = list(y = c(-50, 200),
                       x = c(-10, 75)))

# note that when fixing range and expanding, range is fixed
# after expansions are applied, so effectively the larger expansions apply to both.
# compare the following output to the previous output:
axis_limits(
  p,
  expand_limits = list(y = c(-50, 200),
                       x = c(-10, 75)),
  axis_limits_equal = TRUE)

MiguelRodo/plotutils documentation built on Nov. 7, 2023, 5:18 p.m.