Repository navigation
Layer parameter controlling facet layout - #6336
Conversation
|
Consider:
|
|
Update:
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 |
| # 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))] | ||
| } | ||
| } |
There was a problem hiding this comment.
This part is new
| # Filter panels when layer_layout is an integer | ||
| if (is_integerish(layer_layout)) { | ||
| data <- vec_slice(data, data$PANEL %in% layer_layout) | ||
| } |
There was a problem hiding this comment.
This part is new
| include_margins <- !isFALSE(params$margin %||% FALSE) && | ||
| nrow(facet_vals) == nrow(data) && grid_layout | ||
| if (include_margins) { | ||
| # Margins are computed on evaluated faceting values (#1864). |
There was a problem hiding this comment.
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.
| 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) | ||
| } |
thomasp85
left a comment
There was a problem hiding this comment.
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?

This PR aims to (eventually) fix #3062.
Briefly, it adds a
layer(layout)argument to control how data is assigned to panels. Currently onlylayout = "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.Created on 2025-02-18 with reprex v2.1.1