Skip to content

Facet: gain draw_facet_panels method to draw grobs for each panel #6421

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* (internal) New `Facet$draw_panel_content()` method for delegating panel
assembly (@Yunuuuu, #6406).
* Facet gains a new method `setup_panel_params` to interact with the
panel_params setted by Coord object (@Yunuuuu, #6397, #6380)
* `position_fill()` avoids stacking observations of zero (@teunbrand, #6338)
Expand Down
32 changes: 32 additions & 0 deletions R/facet-.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ NULL
#' between the layer stack and the foreground defined by the Coord object
#' (usually empty). The default is, as above, to return an empty grob.
#'
#' - `draw_panel_content`: Draws each panel for the facet. Should return a list
#' of grobs, one for each panel. The output is used by the `draw_panels`
#' method.
#'
#' - `draw_labels`: Given the gtable returned by `draw_panels`,
#' add axis titles to the gtable. The default is to add one title at each side
#' depending on the position and existence of axes.
Expand Down Expand Up @@ -138,6 +142,34 @@ Facet <- ggproto("Facet", NULL,
draw_front = function(data, layout, x_scales, y_scales, theme, params) {
rep(list(zeroGrob()), vec_unique_count(layout$PANEL))
},
draw_panel_content = function(self, panels, layout, x_scales, y_scales,
ranges, coord, data, theme, params, ...) {
facet_bg <- self$draw_back(
data,
layout,
x_scales,
y_scales,
theme,
params
)
facet_fg <- self$draw_front(
data,
layout,
x_scales,
y_scales,
theme,
params
)

# Draw individual panels, then call `$draw_panels()` method to
# assemble into gtable
lapply(seq_along(panels[[1]]), function(i) {
panel <- lapply(panels, `[[`, i)
panel <- c(facet_bg[i], panel, facet_fg[i])
panel <- coord$draw_panel(panel, ranges[[i]], theme)
ggname(paste("panel", i, sep = "-"), panel)
})
},
draw_panels = function(self, panels, layout, x_scales = NULL, y_scales = NULL,
ranges, coord, data = NULL, theme, params) {

Expand Down
20 changes: 4 additions & 16 deletions R/layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,17 @@ Layout <- ggproto("Layout", NULL,
# Assemble the facet fg & bg, the coord fg & bg, and the layers
# Returns a gtable
render = function(self, panels, data, theme, labels) {
facet_bg <- self$facet$draw_back(data,
panels <- self$facet$draw_panel_content(
panels,
self$layout,
self$panel_scales_x,
self$panel_scales_y,
theme,
self$facet_params
)
facet_fg <- self$facet$draw_front(
self$panel_params,
self$coord,
data,
self$layout,
self$panel_scales_x,
self$panel_scales_y,
theme,
self$facet_params
)

# Draw individual panels, then assemble into gtable
panels <- lapply(seq_along(panels[[1]]), function(i) {
panel <- lapply(panels, `[[`, i)
panel <- c(facet_bg[i], panel, facet_fg[i])
panel <- self$coord$draw_panel(panel, self$panel_params[[i]], theme)
ggname(paste("panel", i, sep = "-"), panel)
})
plot_table <- self$facet$draw_panels(
panels,
self$layout,
Expand Down
3 changes: 3 additions & 0 deletions man/ggplot2-ggproto.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.