Skip to content

Commit 154358b

Browse files
committed
add function layout_theme() and layout_annotation()
1 parent 2af7b02 commit 154358b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+561
-196
lines changed

NAMESPACE

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ S3method(alignpatch,spacer)
3535
S3method(alignpatch,trellis)
3636
S3method(alignpatch,wrapped_patch)
3737
S3method(alignpatch,wrapped_plot)
38-
S3method(as.list,alignpatches)
3938
S3method(as_areas,"NULL")
4039
S3method(as_areas,align_area)
4140
S3method(as_areas,character)
@@ -90,16 +89,15 @@ S3method(grid.draw,patch_ggplot)
9089
S3method(heatmap_add,facetted_pos_scales)
9190
S3method(heatmap_add,gg)
9291
S3method(heatmap_add,labels)
92+
S3method(heatmap_layout,"NULL")
93+
S3method(heatmap_layout,default)
94+
S3method(heatmap_layout,formula)
95+
S3method(heatmap_layout,functon)
96+
S3method(heatmap_layout,matrix)
9397
S3method(layout_add,HeatmapLayout)
9498
S3method(layout_add,StackLayout)
9599
S3method(layout_and_add,HeatmapLayout)
96100
S3method(layout_and_add,StackLayout)
97-
S3method(layout_heatmap,"NULL")
98-
S3method(layout_heatmap,default)
99-
S3method(layout_heatmap,formula)
100-
S3method(layout_heatmap,functon)
101-
S3method(layout_heatmap,matrix)
102-
S3method(layout_heatmap_add,"NULL")
103101
S3method(layout_heatmap_add,Align)
104102
S3method(layout_heatmap_add,data.frame)
105103
S3method(layout_heatmap_add,default)
@@ -120,13 +118,6 @@ S3method(layout_heatmap_subtract,facetted_pos_scales)
120118
S3method(layout_heatmap_subtract,gg)
121119
S3method(layout_heatmap_subtract,ggplot)
122120
S3method(layout_heatmap_subtract,labels)
123-
S3method(layout_stack,"NULL")
124-
S3method(layout_stack,character)
125-
S3method(layout_stack,data.frame)
126-
S3method(layout_stack,default)
127-
S3method(layout_stack,matrix)
128-
S3method(layout_stack,numeric)
129-
S3method(layout_stack_add,"NULL")
130121
S3method(layout_stack_add,Align)
131122
S3method(layout_stack_add,HeatmapLayout)
132123
S3method(layout_stack_add,default)
@@ -180,6 +171,12 @@ S3method(set_context,HeatmapLayout)
180171
S3method(set_context,StackLayout)
181172
S3method(set_layout,HeatmapLayout)
182173
S3method(set_layout,StackLayout)
174+
S3method(stack_layout,"NULL")
175+
S3method(stack_layout,character)
176+
S3method(stack_layout,data.frame)
177+
S3method(stack_layout,default)
178+
S3method(stack_layout,matrix)
179+
S3method(stack_layout,numeric)
183180
export(Align)
184181
export(GeomDraw)
185182
export(align)
@@ -207,16 +204,18 @@ export(ggpanel)
207204
export(ggstack)
208205
export(gpar)
209206
export(hclust2)
207+
export(heatmap_layout)
210208
export(hmanno)
211209
export(inset)
212210
export(is.ggheatmap)
213211
export(is.ggstack)
214-
export(layout_heatmap)
215-
export(layout_stack)
212+
export(layout_annotation)
213+
export(layout_theme)
216214
export(patch)
217215
export(patch_titles)
218216
export(read_example)
219217
export(stack_active)
218+
export(stack_layout)
220219
export(unit)
221220
export(wrap)
222221
exportMethods("$")

R/activate.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
#' Used to modify the data after layout has been created, but before the data is
3030
#' handled of to the ggplot2 for rendering. Use this hook if the you needs
3131
#' change the default data for all `geoms`.
32-
#' @param theme `r rd_theme()` Only used when position is `NULL`.
32+
#' @param theme `r rd_layout_theme()` Only used when position is `NULL`.
3333
#' @param what What should get activated for the anntoation stack? Only used
3434
#' when position is not `NULL`. See [stack_active] for details.
3535
#' @return A `heatmap_active` object which can be added into
36-
#' [HeatmapLayout][layout_heatmap].
36+
#' [HeatmapLayout][heatmap_layout].
3737
#' @examples
3838
#' ggheatmap(matrix(rnorm(81), nrow = 9)) +
3939
#' hmanno("top") +
@@ -86,11 +86,11 @@ hmanno <- function(position = NULL, size = NULL, width = NULL, height = NULL,
8686
#' Usually you are waive to use this, since the adding procedure can be
8787
#' easily changed.
8888
#' * `NULL`: Remove any active context, this is useful when the active
89-
#' context is a [layout_heatmap()] object, where any `Align` objects will
89+
#' context is a [heatmap_layout()] object, where any `Align` objects will
9090
#' be added into the heatmap. By removing the active context, we can add
91-
#' `Align` object into the [layout_stack()] .
91+
#' `Align` object into the [stack_layout()] .
9292
#' @return A `stack_active` object which can be added into
93-
#' [StackLayout][layout_stack].
93+
#' [StackLayout][stack_layout].
9494
#' @examples
9595
#' ggstack(matrix(1:9, nrow = 3L)) +
9696
#' ggheatmap() +

R/align-.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
#' the observations. It means the `NROW(data)` must return the same number with
1515
#' the parallel `layout` axis.
1616
#'
17-
#' - `layout_heatmap`: for column annotation, the `layout` data will be
17+
#' - `heatmap_layout`: for column annotation, the `layout` data will be
1818
#' transposed before using (If data is a `function`, it will be applied with
1919
#' the transposed matrix). This is necessary because column annotation uses
2020
#' heatmap columns as observations, but we need rows.
2121
#'
22-
#' - `layout_stack`: the `layout` data will be used as it is since we place all
22+
#' - `stack_layout`: the `layout` data will be used as it is since we place all
2323
#' plots along a single axis.
2424
#'
2525
#' @param size Plot size, can be an [unit][grid::unit] object.

R/alignpatch-align_plots.R

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#' @param guides Which guide should be collected? A string containing one or
1818
#' more of `r rd_values(.tlbr)`.
1919
#' @inheritParams ggplot2::labs
20-
#' @param theme `r rd_theme()`
20+
#' @param theme `r rd_layout_theme()`
2121
#' @return A `alignpatches` object.
2222
#' @examples
2323
#' # directly copied from patchwork
@@ -52,11 +52,6 @@ align_plots <- function(..., ncol = NULL, nrow = NULL, byrow = TRUE,
5252
title = NULL, subtitle = NULL, caption = NULL,
5353
theme = NULL) {
5454
plots <- rlang::dots_list(..., .ignore_empty = "all")
55-
assert_bool(byrow)
56-
if (!is.null(guides)) {
57-
assert_position(guides)
58-
guides <- setup_position(guides)
59-
}
6055
assert_s3_class(theme, "theme", null_ok = TRUE)
6156
nms <- names(plots)
6257
if (!is.null(nms) && is.character(design)) {
@@ -71,27 +66,85 @@ align_plots <- function(..., ncol = NULL, nrow = NULL, byrow = TRUE,
7166
}
7267
design <- as_areas(design)
7368
patches <- lapply(plots, alignpatch)
74-
new_alignpatches(patches, list(
69+
new_alignpatches(patches,
70+
design = layout_design(
71+
ncol = ncol,
72+
nrow = nrow,
73+
byrow = byrow,
74+
widths = widths,
75+
heights = heights,
76+
design = design,
77+
guides = guides
78+
),
79+
annotation = layout_annotation(
80+
title = title,
81+
subtitle = subtitle,
82+
caption = caption
83+
),
84+
theme = theme
85+
)
86+
}
87+
88+
new_alignpatches <- function(patches, design, annotation, theme) {
89+
structure(
90+
list(
91+
patches = patches, design = design,
92+
annotation = annotation, theme = theme
93+
),
94+
# Will ensure serialisation includes a link to the `ggalign`
95+
# namespace
96+
`_namespace` = namespace_link,
97+
class = "alignpatches"
98+
)
99+
}
100+
101+
#############################################################
102+
#' @inherit patchwork::plot_layout
103+
#' @inheritParams patchwork::plot_layout
104+
#' @importFrom ggplot2 waiver
105+
#' @noRd
106+
layout_design <- function(ncol = waiver(), nrow = waiver(), byrow = waiver(),
107+
widths = waiver(), heights = waiver(),
108+
design = waiver(), guides = waiver()) {
109+
if (!is.waive(byrow)) assert_bool(byrow)
110+
if (!is.waive(design)) design <- as_areas(design)
111+
if (!is.waive(guides) && !is.null(guides)) {
112+
assert_position(guides)
113+
guides <- setup_position(guides)
114+
}
115+
structure(list(
75116
ncol = ncol,
76117
nrow = nrow,
77118
byrow = byrow,
78119
widths = widths,
79120
heights = heights,
80121
design = design,
81-
guides = guides,
82-
title = title, subtitle = subtitle, caption = caption
83-
), theme = theme)
122+
guides = guides
123+
), class = c("layout_design", "plot_layout"))
84124
}
85125

86-
new_alignpatches <- function(patches, layout, theme) {
126+
#' Annotate the whole layout
127+
#'
128+
#' @inheritParams ggplot2::labs
129+
#' @return A `layout_annotation` object to be added into `r rd_layout()`.
130+
#' @importFrom ggplot2 waiver
131+
#' @export
132+
layout_annotation <- function(title = waiver(), subtitle = waiver(),
133+
caption = waiver()) {
87134
structure(
88-
list(patches = patches, layout = layout, theme = theme),
89-
# Will ensure serialisation includes a link to the `ggalign`
90-
# namespace
91-
`_namespace` = ggalign_namespace_link,
92-
class = "alignpatches"
135+
list(title = title, subtitle = subtitle, caption = caption),
136+
class = c("layout_annotation", "plot_annotation")
93137
)
94138
}
95139

140+
#' Modify components of the layout theme
141+
#' @inherit ggplot2::theme description sections references author source note format
142+
#' @inheritDotParams ggplot2::theme
143+
#' @note Only used to render the `guides`, `title`, `subtitle`, `caption`,
144+
#' `margins` and `background`.
145+
#' @return A `layout_theme` object to be added into `r rd_layout()`.
146+
#' @examples
147+
#' layout_theme(plot.background = element_rect(fill = "green"))
148+
#' @importFrom ggplot2 theme
96149
#' @export
97-
as.list.alignpatches <- function(x, ...) .subset2(x, "patches")
150+
layout_theme <- function(...) add_class(ggplot2::theme(...), "layout_theme")

R/alignpatch-alignpatches.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ PatchAlignpatches <- ggproto("PatchAlignpatches", Patch,
1111
#' @importFrom ggplot2 wrap_dims calc_element zeroGrob
1212
patch_gtable = function(self, guides, plot = self$plot) {
1313
patches <- .subset2(plot, "patches")
14-
layout <- .subset2(plot, "layout")
14+
layout <- .subset2(plot, "design")
1515

1616
# complete the theme object
1717
theme <- complete_theme(.subset2(plot, "theme"))

R/alignpatch-build.R

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,17 @@ grid.draw.alignpatches <- function(x, recording = TRUE) {
4444
#' @export
4545
ggalign_build.alignpatches <- function(x) x
4646

47-
#' @importFrom ggplot2 find_panel element_render theme
47+
#' @importFrom ggplot2 find_panel element_render theme theme_get
4848
#' @importFrom gtable gtable_add_grob gtable_add_rows gtable_add_cols
4949
#' @importFrom rlang arg_match0
5050
#' @export
5151
ggalign_gtable.alignpatches <- function(x) {
52-
layout <- .subset2(x, "layout")
53-
theme <- .subset2(x, "theme")
52+
annotation <- .subset2(x, "annotation")
5453

55-
# use complete_theme() when ggplot2 release
54+
# ensure theme has no missing value
55+
theme <- .subset2(x, "theme") %||% theme_get()
56+
57+
# `TODO`: use `complete_theme()` from ggplot2 release
5658
theme <- complete_theme(theme)
5759
x$theme <- theme
5860
table <- alignpatch(x)$patch_gtable()
@@ -62,21 +64,22 @@ ggalign_gtable.alignpatches <- function(x) {
6264
# Add title, subtitle, and caption -------------------
6365
# https://github.com/tidyverse/ggplot2/blob/2e08bba0910c11a46b6de9e375fade78b75d10dc/R/plot-build.R#L219C3-L219C9
6466
title <- element_render(
65-
theme, "plot.title", .subset2(layout, "title"),
67+
theme = theme, "plot.title",
68+
.subset2(annotation, "title"),
6669
margin_y = TRUE, margin_x = TRUE
6770
)
6871
title_height <- grobHeight(title)
6972

7073
# Subtitle
7174
subtitle <- element_render(
72-
theme, "plot.subtitle", .subset2(layout, "subtitle"),
75+
theme, "plot.subtitle", .subset2(annotation, "subtitle"),
7376
margin_y = TRUE, margin_x = TRUE
7477
)
7578
subtitle_height <- grobHeight(subtitle)
7679

7780
# whole plot annotation
7881
caption <- element_render(
79-
theme, "plot.caption", .subset2(layout, "caption"),
82+
theme, "plot.caption", .subset2(annotation, "caption"),
8083
margin_y = TRUE, margin_x = TRUE
8184
)
8285
caption_height <- grobHeight(caption)

0 commit comments

Comments
 (0)