Skip to content

Feature request: Inert out-of-bounds function #255

@teunbrand

Description

@teunbrand

I think it might be useful to have an out-of-bounds function similar to censor(), squish() and squish_infite() that does nothing to the data. The function can be very simple and trivial (probably could use a better name though):

inert_oob <- function(x, ...) x

It is very similar to base::identity and base::force but these don't take additional arguments and raise errors when used as oob-arguments in ggplot2. I'm sure a function like this must exist somewhere, but maybe my R vocabulary isn't large enough.

The use case for this in the ggplot2 package, would be to achieve the same what the coordinate limits do to the plot.

To demonstrate this, notice that the following plots are equivalent:

df <- data.frame(
  x = 1:3,
  y = c(1, 10, Inf)
)

ggplot(df, aes(x, y)) +
  geom_col() +
  coord_cartesian(ylim = c(0, 5))

image

ggplot(df, aes(x, y)) +
  geom_col() +
  scale_y_continuous(
    limits = c(0, 5),
    oob = inert_oob
  )

image

I've seen on quite a number of stack overflow posts that people are confused why they must use the coordinate limits and not the scale limits or can't figure out why their barplots dissappear when the lower-limit on y is > 0 (for example: https://stackoverflow.com/questions/58954151/problems-adapting-the-y-axis-to-2x2-anova-bargraph-using-r-and-ggplot/)

Formalising this behaviour of the scale limits as a function might help people better understand that not touching the data is a valid option for axis limits too.

If this function already exists somewhere, maybe it might be best to ignore this issue and I should ask the people at ggplot2 whether they would consider to document this use somewhere.

Thank you for taking the time to read!

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions