@@ -145,19 +145,40 @@ S7::method(patch, alignpatches) <- function(x) {
145145
146146# #############################################################
147147# For z in the gtable layout
148+ # 0L: layout background
149+ # 1L: background of the plot
150+ # 2L: plot table
151+ # 3L: foreground of the panel area
152+ # 4L: legends
153+ # 5L: tags
148154LAYOUT_BACKGROUND_Z <- 0L
149155PLOT_BACKGROUND_Z <- 1L
150156PLOT_TABLE_Z <- 2L
151157LAYOUT_FOREGROUND_Z <- 3L
152158GUIDE_LEGENDS_Z <- 4L
153159TAGS_Z <- 5L
154160
155- # 0L: layout background
156- # 1L: background of the plot
157- # 2L: plot table
158- # 3L: foreground of the panel area
159- # 4L: legends
160- # 5L: tags
161+ # ' Options passed to the Patch `gtable` method
162+ # '
163+ # ' This class defines the options that can be passed to the `gtable` method of a
164+ # ' `Patch` object. It includes:
165+ # '
166+ # ' - `theme`: The theme to be applied, which can be either `NULL` or a ggplot2
167+ # ' [theme][ggplot2::theme] object.
168+ # ' - `guides`: The guides for the plot, which can be `NULL` or a character
169+ # ' vector.
170+ # ' - `tagger`: Either `NULL` (no tagging) or a `LayoutTagger` object that
171+ # ' provides a `$tag_table` method (accepting the `gtable` and `theme`)
172+ # ' used to add tag.
173+ # '
174+ # ' @keywords internal
175+ patch_options <- S7 :: new_class(" patch_options" ,
176+ properties = list (
177+ theme = S7 :: new_union(NULL , ggplot2 :: class_theme ),
178+ guides = S7 :: new_union(NULL , S7 :: class_character ),
179+ tagger = S7 :: new_union(NULL , S7 :: new_S3_class(" ggalign::LayoutTagger" ))
180+ )
181+ )
161182
162183# ' @importFrom ggplot2 ggproto
163184# ' @noRd
@@ -166,12 +187,12 @@ PatchAlignpatches <- ggproto(
166187 # ' @importFrom gtable gtable gtable_add_grob
167188 # ' @importFrom grid unit
168189 # ' @importFrom ggplot2 wrap_dims calc_element zeroGrob theme_get
169- # ' @importFrom S7 prop
170- gtable = function (self , theme = NULL , guides = NULL , tagger = NULL ) {
190+ # ' @importFrom S7 prop prop<- set_props
191+ gtable = function (self , options ) {
171192 patches <- lapply(prop(self $ plot , " plots" ), function (p ) {
172193 out <- patch(p )
173194 if (! is.null(out ) &&
174- ! inherits(out , sprintf( " %s ::Patch" , pkg_nm()) )) {
195+ ! inherits(out , " ggalign ::Patch" )) {
175196 cli_abort(" {.fn alignpatch} must return a {.cls Patch} object" )
176197 }
177198 out
@@ -234,7 +255,8 @@ PatchAlignpatches <- ggproto(
234255 }
235256
236257 # we define the global theme --------------------------
237- if (is.null(theme )) { # No parent theme provided
258+ # No parent theme provided
259+ if (is.null(theme <- prop(options , " theme" ))) {
238260 top_level <- TRUE
239261 # by default, we use ggplot2 default theme
240262 theme <- prop(self $ plot , " theme" )
@@ -243,9 +265,10 @@ PatchAlignpatches <- ggproto(
243265 theme <- theme + prop(self $ plot , " theme" )
244266 }
245267 theme <- complete_theme(theme )
268+ prop(options , " theme" , check = FALSE ) <- theme
246269
247270 # by default, we won't collect any guide legends
248- collected <- guides
271+ collected <- prop( options , " guides" )
249272 guides <- prop(layout , " guides" )
250273 if (is_string(guides )) {
251274 guides <- setup_guides(guides )
@@ -268,15 +291,17 @@ PatchAlignpatches <- ggproto(
268291 # - A single string representing the tag for the entire layout,
269292 # - NULL, meaning no tagging,
270293 # - Or a `LayoutTagger` object used to tag each plot individually.
271- tagger <- create_layout_tagger(prop(self $ plot , " tags" ), tagger )
272- if (! is.null(tagger ) && ! inherits(tagger , " LayoutTagger" )) {
273- # If tagger is not a LayoutTagger, treat it as a single tag for the
274- # whole layout
275- tag <- tagger
276- tagger <- NULL
277- } else {
278- # Otherwise, no single tag for the whole layout
294+ tag <- create_layout_tagger(
295+ prop(self $ plot , " tags" ),
296+ prop(options , " tagger" )
297+ )
298+ if (is.null(tag ) || inherits(tag , " ggalign::LayoutTagger" )) {
299+ prop(options , " tagger" , check = FALSE ) <- tag
279300 tag <- NULL
301+ } else {
302+ # If tag is a single string, treat it as a single tag for the
303+ # whole layout
304+ prop(options , " tagger" , check = FALSE ) <- NULL
280305 }
281306
282307 # Let each patch to determine whether to collect guides
@@ -299,7 +324,7 @@ PatchAlignpatches <- ggproto(
299324 .subset2(guides , i ),
300325 intersect(border_with_guides , .subset2(borders_list , i ))
301326 )
302- gt <- patch $ gtable(theme , g , tagger )
327+ gt <- patch $ gtable(set_props( options , guides = g ) )
303328 components <- patch $ decompose_guides(gt , g )
304329 guides_list [i ] <- list (.subset2(components , " guides" ))
305330 gt_list [i ] <- list (.subset2(components , " gt" ))
0 commit comments