Skip to content

Commit 3aec34a

Browse files
Yunuuuuteunbrand
andauthored
Facet: gain draw_panel_content method to draw grobs for each panel (#6421)
* Facet: gain draw_facet_panels method to draw grobs for each panel * renmae `draw_facet_panels` to `draw_panel_content` * rename `Facet$draw_facet_panels` to `Facet$draw_panel_content` * Update R/facet-.R Co-authored-by: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> * add news bullet --------- Co-authored-by: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Co-authored-by: Teun van den Brand <tahvdbrand@gmail.com>
1 parent 61b9ebc commit 3aec34a

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* (internal) New `Facet$draw_panel_content()` method for delegating panel
4+
assembly (@Yunuuuu, #6406).
35
* Facet gains a new method `setup_panel_params` to interact with the
46
panel_params setted by Coord object (@Yunuuuu, #6397, #6380)
57
* `position_fill()` avoids stacking observations of zero (@teunbrand, #6338)

R/facet-.R

+32
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ NULL
6868
#' between the layer stack and the foreground defined by the Coord object
6969
#' (usually empty). The default is, as above, to return an empty grob.
7070
#'
71+
#' - `draw_panel_content`: Draws each panel for the facet. Should return a list
72+
#' of grobs, one for each panel. The output is used by the `draw_panels`
73+
#' method.
74+
#'
7175
#' - `draw_labels`: Given the gtable returned by `draw_panels`,
7276
#' add axis titles to the gtable. The default is to add one title at each side
7377
#' depending on the position and existence of axes.
@@ -138,6 +142,34 @@ Facet <- ggproto("Facet", NULL,
138142
draw_front = function(data, layout, x_scales, y_scales, theme, params) {
139143
rep(list(zeroGrob()), vec_unique_count(layout$PANEL))
140144
},
145+
draw_panel_content = function(self, panels, layout, x_scales, y_scales,
146+
ranges, coord, data, theme, params, ...) {
147+
facet_bg <- self$draw_back(
148+
data,
149+
layout,
150+
x_scales,
151+
y_scales,
152+
theme,
153+
params
154+
)
155+
facet_fg <- self$draw_front(
156+
data,
157+
layout,
158+
x_scales,
159+
y_scales,
160+
theme,
161+
params
162+
)
163+
164+
# Draw individual panels, then call `$draw_panels()` method to
165+
# assemble into gtable
166+
lapply(seq_along(panels[[1]]), function(i) {
167+
panel <- lapply(panels, `[[`, i)
168+
panel <- c(facet_bg[i], panel, facet_fg[i])
169+
panel <- coord$draw_panel(panel, ranges[[i]], theme)
170+
ggname(paste("panel", i, sep = "-"), panel)
171+
})
172+
},
141173
draw_panels = function(self, panels, layout, x_scales = NULL, y_scales = NULL,
142174
ranges, coord, data = NULL, theme, params) {
143175

R/layout.R

+4-16
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,17 @@ Layout <- ggproto("Layout", NULL,
6060
# Assemble the facet fg & bg, the coord fg & bg, and the layers
6161
# Returns a gtable
6262
render = function(self, panels, data, theme, labels) {
63-
facet_bg <- self$facet$draw_back(data,
63+
panels <- self$facet$draw_panel_content(
64+
panels,
6465
self$layout,
6566
self$panel_scales_x,
6667
self$panel_scales_y,
67-
theme,
68-
self$facet_params
69-
)
70-
facet_fg <- self$facet$draw_front(
68+
self$panel_params,
69+
self$coord,
7170
data,
72-
self$layout,
73-
self$panel_scales_x,
74-
self$panel_scales_y,
7571
theme,
7672
self$facet_params
7773
)
78-
79-
# Draw individual panels, then assemble into gtable
80-
panels <- lapply(seq_along(panels[[1]]), function(i) {
81-
panel <- lapply(panels, `[[`, i)
82-
panel <- c(facet_bg[i], panel, facet_fg[i])
83-
panel <- self$coord$draw_panel(panel, self$panel_params[[i]], theme)
84-
ggname(paste("panel", i, sep = "-"), panel)
85-
})
8674
plot_table <- self$facet$draw_panels(
8775
panels,
8876
self$layout,

man/ggplot2-ggproto.Rd

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)