tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.39k stars 2k forks source link

`facet_grid()` evaluates facets before adding margins #5944

Open teunbrand opened 3 weeks ago

teunbrand commented 3 weeks ago

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