Skip to content

Commit 2fe2f42

Browse files
author
Yunuuuu
committed
refactor: ensure consistent use of theme option during plot construction
1 parent 409390a commit 2fe2f42

3 files changed

Lines changed: 60 additions & 56 deletions

File tree

R/alignpatch-alignpatches.R

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,12 @@ TAGS_Z <- 5L
167167
#' [theme][ggplot2::theme] object.
168168
#' - `guides`: The guides for the plot, which can be `NULL` or a character
169169
#' vector.
170-
#' - `tag`: Either `NULL` (no tag), a single string, or a `LayoutTagger` object
171-
#' that provides a `$tag()` method to generate a tag string for tagging each
172-
#' plot individually.
170+
#' - `tag`: Can be `NULL` (no tag), a single string, or a `LayoutTagger` object
171+
#' that provides a `$tag()` method to generate a tag string for each plot.
172+
#' The `LayoutTagger` is used specifically by [`alignpatches()`]. For
173+
#' individual plots, you typically call the `$tag()` method of the
174+
#' `LayoutTagger` object to return a string, which triggers the internal
175+
#' `Patch$tag()` method to add a tag.
173176
#' - `metadata`: A list of additional metadata. This can store any extra
174177
#' information relevant to the patch, such as custom parameters or auxiliary
175178
#' data that do not fit into the other categories.
@@ -240,36 +243,30 @@ PatchAlignpatches <- ggproto(
240243
# remove NULL patch -----------------------------------
241244
keep <- !vapply(patches, is.null, logical(1L), USE.NAMES = FALSE)
242245
patches <- vec_slice(patches, keep)
246+
area <- vec_slice(area, keep)
243247

244-
# if no plots, we do nothing --------------------------
248+
#######################################################
249+
# prepare the final options ---------------------------
245250
options <- options %||% patch_options()
246-
metadata <- list(patches = patches)
247-
if (is_empty(patches)) {
248-
prop(options, "metadata", check = FALSE) <- metadata
249-
return(options)
250-
}
251+
metadata <- list(
252+
area = area,
253+
dims = dims,
254+
panel_widths = panel_widths,
255+
panel_heights = panel_heights,
256+
patches = patches
257+
)
251258

252-
# add borders to patch --------------------------------
253-
area <- vec_slice(area, keep)
254-
borders_list <- vector("list", length(patches))
255-
for (i in seq_along(patches)) {
256-
borders_list[i] <- list(c(
257-
if (field(area, "t")[i] == 1L) "top" else NULL,
258-
if (field(area, "l")[i] == 1L) "left" else NULL,
259-
if (field(area, "b")[i] == .subset(dims, 1L)) {
260-
"bottom"
261-
} else {
262-
NULL
263-
},
264-
if (field(area, "r")[i] == .subset(dims, 2L)) {
265-
"right"
266-
} else {
267-
NULL
268-
}
269-
))
259+
# we define the guides --------------------------------
260+
collected <- prop(options, "guides")
261+
guides <- prop(layout, "guides")
262+
if (is_string(guides)) {
263+
guides <- setup_guides(guides)
264+
} else if (is_waiver(guides)) {
265+
guides <- collected
270266
}
267+
metadata$collected <- collected
268+
prop(options, "guides", check = FALSE) <- guides
271269

272-
#######################################################
273270
# we define the global theme -------------------------
274271
# No parent theme provided
275272
if (is.null(theme <- prop(options, "theme"))) {
@@ -282,15 +279,6 @@ PatchAlignpatches <- ggproto(
282279
}
283280
prop(options, "theme", check = FALSE) <- complete_theme(theme)
284281

285-
collected <- prop(options, "guides")
286-
guides <- prop(layout, "guides")
287-
if (is_string(guides)) {
288-
guides <- setup_guides(guides)
289-
} else if (is_waiver(guides)) {
290-
guides <- collected
291-
}
292-
prop(options, "guides", check = FALSE) <- guides
293-
294282
# A tag can be:
295283
# - A single string representing the tag for the entire layout,
296284
# - NULL, meaning no tagging,
@@ -300,6 +288,10 @@ PatchAlignpatches <- ggproto(
300288
prop(options, "tag")
301289
)
302290

291+
if (is_empty(patches)) {
292+
return(options)
293+
}
294+
303295
#######################################################
304296
# define the options for the sub-plots ----------------
305297
# the `alignpatches` tag is a string, it means regarding the
@@ -315,6 +307,24 @@ PatchAlignpatches <- ggproto(
315307
patch$setup_options(subplots_options)
316308
})
317309

310+
# add borders to patch --------------------------------
311+
borders_list <- vector("list", length(patches))
312+
for (i in seq_along(patches)) {
313+
borders_list[i] <- list(c(
314+
if (field(area, "t")[i] == 1L) "top" else NULL,
315+
if (field(area, "l")[i] == 1L) "left" else NULL,
316+
if (field(area, "b")[i] == .subset(dims, 1L)) {
317+
"bottom"
318+
} else {
319+
NULL
320+
},
321+
if (field(area, "r")[i] == .subset(dims, 2L)) {
322+
"right"
323+
} else {
324+
NULL
325+
}
326+
))
327+
}
318328
# Always ensure that plots placed in a border collect their guides, if
319329
# any guides are to be collected in that border.
320330
# This prevents overlap, unless the guides will be collected by the
@@ -334,20 +344,11 @@ PatchAlignpatches <- ggproto(
334344
prop(options, "guides", check = FALSE) <- g
335345
options
336346
})
347+
metadata$borders_list <- borders_list
348+
metadata$options_list <- options_list
337349

338350
#######################################################
339-
prop(options, "metadata", check = FALSE) <- c(
340-
metadata,
341-
list(
342-
collected = collected,
343-
borders_list = borders_list,
344-
options_list = options_list,
345-
area = area,
346-
dims = dims,
347-
panel_widths = panel_widths,
348-
panel_heights = panel_heights
349-
)
350-
)
351+
prop(options, "metadata", check = FALSE) <- metadata
351352
options
352353
},
353354

@@ -357,11 +358,12 @@ PatchAlignpatches <- ggproto(
357358
#' @importFrom S7 prop prop<-
358359
#' @importFrom rlang arg_match0 is_empty
359360
gtable = function(self, options) {
360-
metadata <- prop(options, "metadata")
361-
if (is.null(.subset2(metadata, "patches"))) {
361+
if (is.null(theme <- prop(options, "theme"))) {
362362
cli_abort("Run `$setup_options()` to initialize the patches first.")
363363
}
364364

365+
metadata <- prop(options, "metadata")
366+
# if no plots, we do nothing --------------------------
365367
if (is_empty(.subset2(metadata, "patches"))) {
366368
return(make_patch_table())
367369
}
@@ -451,7 +453,6 @@ PatchAlignpatches <- ggproto(
451453
setdiff(names(guides_list), metadata$collected)
452454
)
453455
}
454-
theme <- prop(options, "theme")
455456
gt <- self$attach_guide_list(
456457
gt = gt,
457458
guide_list = guides_list,

R/alignpatch-build.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ S7::method(ggalign_gtable, alignpatches) <- function(x) {
1717
table <- p$gtable(options)
1818

1919
# ensure theme has no missing value
20-
theme <- complete_theme(prop(x, "theme") %||% theme_get())
20+
theme <- prop(options, "theme")
2121
matrix_respect <- is.matrix(.subset2(table, "respect"))
2222

2323
# Add title, subtitle, and caption -------------------

man/patch_options.Rd

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)