Skip to content

facet_grid() evaluates facets before adding margins - #5944

Merged
teunbrand merged 4 commits into
tidyverse:mainfrom
teunbrand:facet_map_eval_before_margins
Jul 12, 2024
Merged

teunbrand merged 4 commits into
tidyverse:mainfrom
teunbrand:facet_map_eval_before_margins

Conversation

@teunbrand

@teunbrand teunbrand commented Jun 14, 2024 •

Copy link
Copy Markdown
Collaborator

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

@thomasp85 thomasp85 left a comment

Copy link
Copy Markdown
Member

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
@teunbrand
teunbrand deleted the facet_map_eval_before_margins branch July 12, 2024 07:58
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.

facet_grid with an expression cannot have margins

2 participants