Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: flexible parametrisation of rectangles #5861

Open
teunbrand opened this issue Apr 24, 2024 · 0 comments · May be fixed by #5862
Open

Feature request: flexible parametrisation of rectangles #5861

teunbrand opened this issue Apr 24, 2024 · 0 comments · May be fixed by #5862
Labels
feature a feature request or enhancement layers 📈

Comments

@teunbrand
Copy link
Collaborator

Currently we have the following parametrisation for rectangles:

geom_tile() has x, y, width and height.
geom_rect() has xmin, xmax, ymin and ymax.

If you know two of the x or y related aesthetics, you can compute the rest regardless of which two are known. For example, if you know xmin and width, you can compute xmax = xmin + width.
Being able to abitrarily pick two aesthetics with the others imputed allows for more flexibility. For example the following two options for visualising the presidential dataset are both horrifying.

Using geom_rect() option, you're forced to manually resolve a discrete y into a continuous y and hack a continuous scale to look like a discrete scale:

library(ggplot2)

ggplot(presidential) +
  geom_rect(
    aes(xmin = start, 
        xmax = end, 
        ymin = match(name, unique(name)) - 0.4, 
        ymax = match(name, unique(name)) + 0.4)) +
  scale_y_continuous(
    breaks = seq_along(unique(presidential$name)),
    labels = unique(presidential$name)
  ) +
  scale_x_date()

Using the geom_tile() option you're forced to reparametrise the intervals, which can be a real pain for some classed objects like dates.

ggplot(presidential) +
  geom_tile(
    aes(x = structure((unclass(start) + unclass(end)) / 2, class = "Date"),
        y = name,
        width = end - start),
    height = 0.8
  ) +
  scale_y_discrete(
    limits = unique(presidential$name)
  )
#> Don't know how to automatically pick scale for object of type <difftime>.
#> Defaulting to continuous.

Created on 2024-04-24 with reprex v2.1.0

In the above cases, the ideal parameterisation is xmin = start, xmax = end, y = name, height = 0.8, but this currently isn't possible. Besides this, sometimes you might also wish to use mixed parameters and use, for example, ymin and height together.

@teunbrand teunbrand added feature a feature request or enhancement layers 📈 labels Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement layers 📈
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant