Skip to content
New issue

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

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

Already on GitHub? # to your account

facet_grid() evaluates facets before adding margins #5944

Merged
merged 4 commits into from
Jul 12, 2024

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Jun 14, 2024

This PR aims to fix #1864.

Briefly, we evaluate facet variables before adding margins and keep track of margins via an index.

To explain a little; the issue results from (1) margins requiring all faceting variables to be present and (2) faceting values require all margins to be computed. We cannot satisfy both due to their interdependence. This PR solves the issue by evaluating all faceting variables before adding margins, thus satisfying (1) but not (2). The reason for requiring (2) is just so that data is recycled correctly along with the margins. In this PR, we use an index column to keep track of how the original data should be recycled to fit the margins.

Reprex from issue:

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

ggplot(mtcars, aes(wt, mpg)) + 
  geom_point() + 
  facet_grid(vs~I(am+1), margins=T)

Created on 2024-06-14 with reprex v2.1.0

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 teunbrand merged commit f5782ff into tidyverse:main Jul 12, 2024
11 checks passed
@teunbrand teunbrand deleted the facet_map_eval_before_margins branch July 12, 2024 07:58
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

facet_grid with an expression cannot have margins
2 participants