Skip to content

Layer parameter controlling facet layout - #6336

Merged
teunbrand merged 10 commits into
tidyverse:mainfrom
teunbrand:freeze_panels
Mar 25, 2025
Merged

teunbrand merged 10 commits into
tidyverse:mainfrom
teunbrand:freeze_panels

Conversation

@teunbrand

Copy link
Copy Markdown
Collaborator

This PR aims to (eventually) fix #3062.

Briefly, it adds a layer(layout) argument to control how data is assigned to panels. Currently only layout = "fixed" is implemented, which repeats data over every panel. This PR is considered WIP because there might be other strategies that might also be interesting to discuss.

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

ggplot(mpg, aes(displ, hwy)) +
  geom_point(colour = "grey", layout = "fixed") +
  geom_point() +
  facet_grid(year ~ drv)

Created on 2025-02-18 with reprex v2.1.1

@teunbrand

Copy link
Copy Markdown
Collaborator Author

Consider:

  • Using integers to indicate panels

@teunbrand

teunbrand commented Feb 26, 2025 •

Copy link
Copy Markdown
Collaborator Author

Update:

  • Added fixed_cols/fixed_rows options to interact with facet_grid()
  • Added integer option.
devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

ggplot(mpg, aes(displ, hwy)) +
  geom_point(colour = "grey", layout = "fixed_cols") +
  geom_point() +
  annotate(
    "text", I(0.5), I(0.8), label = "My annotation",
    layout = 5
  ) +
  facet_grid(year ~ drv)

Created on 2025-02-26 with reprex v2.1.1

Should be ready for review

@teunbrand teunbrand changed the title WIP: layer parameter controlling facet layout Layer parameter controlling facet layout Feb 26, 2025
Comment thread R/facet-.R Outdated
Comment on lines +132 to +141
# If we need to fix rows or columns, we make the corresponding faceting
# variables missing on purpose
if (grid_layout) {
if (identical(layer_layout, "fixed_rows")) {
facet_vals <- facet_vals[setdiff(names(facet_vals), names(params$cols))]
}
if (identical(layer_layout, "fixed_cols")) {
facet_vals <- facet_vals[setdiff(names(facet_vals), names(params$rows))]
}
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This part is new

Comment thread R/facet-.R Outdated
Comment on lines +174 to +177
# Filter panels when layer_layout is an integer
if (is_integerish(layer_layout)) {
data <- vec_slice(data, data$PANEL %in% layer_layout)
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This part is new

Comment thread R/facet-.R Outdated
Comment on lines +115 to +118
include_margins <- !isFALSE(params$margin %||% FALSE) &&
nrow(facet_vals) == nrow(data) && grid_layout
if (include_margins) {
# Margins are computed on evaluated faceting values (#1864).

@teunbrand teunbrand Feb 26, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I merged the wrap/grid layout mapping because it reduces duplication and the only tangible difference was dealing with margins. Because this makes the diff harder to read, I've put comments at which parts weren't in here before.

Comment thread R/facet-.R Outdated
Comment on lines +103 to +110
grid_layout <- all(c("rows", "cols") %in% names(params))
layer_layout <- attr(data, "layout")
if (identical(layer_layout, "fixed")) {
n <- vec_size(data)
data <- vec_rep(data, vec_size(layout))
data$PANEL <- vec_rep_each(layout$PANEL, n)
return(data)
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is new

@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.

I do like that logic for grid and wrap has been merged but I don't think it should live as a method in the virtual Facet class.

It assumes existence of properties in params that is not "global"

Can we move this to a free function that both facet_grid() and facet_wrap() calls?

@teunbrand
teunbrand merged commit 428d122 into tidyverse:main Mar 25, 2025
@teunbrand
teunbrand deleted the freeze_panels branch March 25, 2025 13:57
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.

Consider adding "static" parameter to geom/stat functions

2 participants