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 " )
0 commit comments