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

geom_rect() can derive corners from x/width or y/height #5862

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #5861.

Briefly, geom_rect() can derive the xmin/xmax/ymin/ymax parameters from x/width and y/height parameters.

The trickiest thing with this PR is what is understood as 'required aesthetics'. Currently, all the parameters mentioned above are 'optional aesthetics' and the corners ( xmin/xmax/ymin/ymax) are marked as 'non missing aesthetics'. The corners are subjected to a second check_required_aesthetics() to throw a warning.

A demo from the issue:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

p <- ggplot(
  presidential,
  aes(xmin = start, xmax = end, y = forcats::fct(name))
)

p + geom_rect(height = 0.8)

However, if there is only 1 known aesthetic per position (in the example, just the y aesthetic), it'll throw an error due to missing aesthetics. It might be worth considering throwing a more tailored warning in this case.

p + geom_rect()
#> Error in `geom_rect()`:
#> ! Problem while setting up geom.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `setup_data()` at ggplot2/R/ggproto.R:190:17:
#> ! `geom_rect()` requires the following missing aesthetics: ymin and
#>   ymax.

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

@teunbrand
Copy link
Collaborator Author

Possibly CI failing due to the R4.4.0 release? Will run again tomorrow.

@teunbrand
Copy link
Collaborator Author

I'm not really content with this PR as it seems like abusing the various aesthetics fields in the ggproto class. I'll put this as a draft for now.

@teunbrand teunbrand marked this pull request as draft April 26, 2024 12:40
@teunbrand

This comment was marked as resolved.

R/geom-rect.R Outdated Show resolved Hide resolved
@teunbrand
Copy link
Collaborator Author

I had to rewrite bits of check_required_aesthetics() to get around the hardcoded limit of 2 paired aesthetics, but I think this now working as it should without aesthetics field shenanigans.

@teunbrand teunbrand marked this pull request as ready for review May 21, 2024 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: flexible parametrisation of rectangles
1 participant