Skip to content

Could xlim and ylim accept a function? #6754

@davidhodge931

Description

@davidhodge931

It is very usefult that the limits argument scale_*_continuous and scale_*_binned functions can accept a function.

It'd be great if xlim and ylim could do this as well

library(tidyverse)

d <- data.frame(
  trt = factor(c(1, 1, 2, 2)),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)
) 

d |>
  ggplot() +
  geom_linerange(
    aes(y = trt,
    x = resp,
    colour = group,
    xmin = lower,
    xmax = upper),
    position = position_dodge(width = 0.2),
  ) 

#works
p +
  xlim(c(0, 5))

#doesn't
p +
  xlim(\(x) range(x, 0))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions