Skip to content

Refactor facet panel drawing code #5917

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

Merged
merged 19 commits into from
Jun 24, 2024
Merged

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented May 30, 2024

This PR aims to fix #5482.

Briefly, the panel drawing code is now divided over 3 Facet ggproto methods: init_gtable(), attach_axes() and attach_strips().

My main gripe with the previous code was that it was hard to follow along with what it was doing. I think reasons why it was hard for me to follow are the following:

  • The code was alternating between the three tasks, for example facet_grid() was first setting up axes, then setting up strips, then initialising the table, then adding the axes and finally adding the strips.
  • Some pieces of code were 4 if blocks deep which is hard to keep track of.

The primary benefit is that it should now be easier to 'just' tweak the axes, or 'just' tweak the strips in extensions.
Quick benchmark showed no speed difference between current main and this PR.

@teunbrand teunbrand mentioned this pull request May 31, 2024
@teunbrand
Copy link
Collaborator Author

TODO: try unifying draw_panels()

@teunbrand
Copy link
Collaborator Author

The draw_panels() method is now part of the base Facet class, as well as the init_gtable() method.
FacetWrap still has to ggproto_parent() the panel drawing code due to interactions with CoordFlip that need to be adressed.
I've left FacetNull as-is, to keep using its highly simplified method.

Comment on lines +150 to +152
if (!is.null(aspect_ratio) && (space$x || space$y)) {
cli::cli_abort("Free scales cannot be mixed with a fixed aspect ratio.")
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't be triggered for FacetWrap as it has no params$space_free, which defaults to list(x = FALSE, y = FALSE).

placement <- if (inside_x) -1 else -2
strip_pad <- axis_size$top
draw_panels = function(self, panels, layout, x_scales, y_scales, ranges, coord, data, theme, params) {
if (inherits(coord, "CoordFlip")) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If failed to find a smarter solution to working out this interaction with CoordFlip, so FacetWrap does this first, then calls the parent method.

@@ -33,7 +33,7 @@

# facet_grid throws errors at bad layout specs

`coord_fixed()` doesn't support free scales.
`facet_grid()` can't use free scales with `coord_fixed()`.
Copy link
Collaborator Author

@teunbrand teunbrand Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wording changed slightly because FacetGrid and FacetWrap now share the method that emits this warning

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand
Copy link
Collaborator Author

teunbrand commented Jun 24, 2024

CI is a bit out of whack because non-MacOS builds use most recent {cli} 3.6.3 whil I presume MacOS still uses an older version.
The new {cli} affects some error message snapshots, which are fine and unrelated to this PR.
I'm going to go ahead and merge this anyway, as I trust that at some point MacOS will also be able to use the new {cli}.

@teunbrand teunbrand merged commit af8e236 into tidyverse:main Jun 24, 2024
10 of 11 checks passed
@teunbrand teunbrand deleted the refactor_panels branch June 24, 2024 08:22
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: Break up FacetX$draw_panels() into smaller functions
2 participants