ggalign::Patch represents the
layout manager for a single subplot within a composite plot. The Patch
object provides the interface for aligning the subplot, managing panel sizes,
and handling guide legends.
Details
In alignpatches(), each subplot is regarded as a patch, and a
corresponding Patch object is required for proper alignment and layout
operations. Patch is a ggproto() object that provides
the core methods for arranging and aligning subplots.
Fields
guidesDescription
(Optional method) Determines which sides of the guide legends should be collected by the parent
alignpatches()object.This per-plot method allows each subplot to modify the
guidespassed to theself$decompose_guides()method, ensuring that plots along the border collect their guide legends correctly. Such fine-grained control cannot be achieved when relying on only a singleself$decompose_guides()method.Arguments
guides: Theguidesargument passed from the parentalignpatches()object, specifying how legends should be combined or positioned. Possible values include"top","left","bottom", and"right".
Value A modified
guidesobject indicating which sides of the guide legends should be collected by the parentalignpatches()object.gtableDescription
(Required method) Constructs a
standardized gtableobject.Arguments
theme: The globalthemeof the parentalignpatches()object.guides: Specifies which sides of guide legends should be collected by the parentalignpatches()object. In most cases, this is the value returned by the subplot'sself$guides()method. For plots along the border, any guide legends on that side will always be collected if any legends on that side of any subplot are being collected.tagger: EitherNULL(no tagging) or aLayoutTaggerobject that provides a$tag_tablemethod (accepting thegtableandtheme) used to add tag.
Value A standardized
gtableobject, or a simplegrobobject.The returned object represents the graphical structure, which can either be a full table-based layout (
gtable) for more complex arrangements or a simpler graphical object (grob) when only basic plot elements are involved.decompose_guidesDescription
(Optional method) Collects guide legends and optionally removes the space they occupy.
This method extracts guide legends based on the sides specified in the
guidesargument. After collecting the guides, the corresponding space in thegtis removed to free up space, except for guides placedinsidethe panel.Arguments
gt: Agtableobject, usually returned byself$gtable().guides: Specifies which sides of guide legends should be collected by the parentalignpatches()object. In most cases, this is the value returned by the subplot'sself$guides()method. For plots along the border, any guide legends on that side will always be collected if any legends on that side of any subplot are being collected.
Value A list with:
gt: The updated gtable with guide legends removed (if applicable).guides: A named list of collected guide grobs corresponding to the sides specified inguides(orNULLif absent).
align_panelDescription
(Optional method) In most cases, panel sizes do not need to be manually adjusted when aligning panels, as long as their border sizes are consistent. However, for gtables with a fixed aspect ratio, this method adjusts the panel width and height based on user input and the dimensions of the underlying gtable (
gt) to ensure proper alignment.When the internal numeric value of either
panel_widthorpanel_heightisNA(i.e.,is.na(as.numeric(...))), that dimension is inferred from the gtable while maintaining the aspect ratio for single-panel layouts whenrespect = TRUE.Arguments
gt: Agtableobject, usually returned byself$decompose_guides().panel_width/panel_height: Unit objects specifying the desired panel size. If the internal numeric value of either isNA, the size is computed from the gtable (gt).
Value A list with components:
width: Final panel width as a unit objectheight: Final panel height as a unit objectrespect: IfTRUE, the aspect ratio was enforced
border_sizesDescription
(Optional method) retrieve the border sizes of a gtable.
Arguments
gt: Agtableobject, usually returned byself$decompose_guides().free: Optional. Borders to exclude when calculating sizes. Possible values include"top","left","bottom", and"right".
Value A list with components:
top:unitvalues for the top borders orNULL.left:unitvalues for the left borders orNULL.bottom:unitvalues for the bottom borders orNULL.right:unitvalues for the right borders orNULL.
align_borderDescription
(Optional method) This method modifies the top, left, bottom, and right border sizes of the underlying gtable (
gt) by replacing corresponding entries in itsheightsandwidthsvectors..Arguments
gt: Agtableobject, usually returned byself$decompose_guides().t,l,b,r: Optional numeric vectors specifying new sizes for the top, left, bottom, and right borders, respectively. Each vector replaces the corresponding entries ingt$heightsorgt$widths.
Value A modified
gtableobject.placeDescription (Optional method) Inserts the patch's gtable (including optional background) into the target canvas gtable.
This method places the patch's gtable into a specified location of another gtable, preserving the background and plot layers separately if a background exists. The
t,l,b,rarguments specify the position in the target gtable, andbg_z/plot_zdefine the stacking order (z-order) for background and plot.Arguments
gtable: the target canvas gtable into which the patch will be inserted.gt: Agtableobject, usually returned byself$align_border().t,l,b,r: Integer positions (top, left, bottom, right) specifying where to insert the patch in the target gtable.i: Index of the current patch, used to generate unique grob names.bg_z: Z-order for the background grob (default1L).plot_z: Z-order for the plot grob (default2L).
Details
If the patch includes a grob named
"background", it is separated from the main plot and inserted independently from the plot grob.If no background is present, the entire gtable is inserted as the plot grob.
Value The modified target canvas gtable with the patch's gtable added.
decompose_bgDescription Separates the background grob (if present) from the main gtable.
Value A list with:
bg: The background grob (orNULLif absent)gt: The gtable with background removed
place_bgDescription Adds the background grob into the target gtable.
place_gtDescription Adds the main plot gtable into the target gtable.
is_alignpatchesDescription
Checks whether the object inherits from the
alignpatches()Patchrepresentation.If
TRUE, the fieldsself$patches,self$gt_list, andself$borders_listare expected to exist in the$align_border()and$place()methods. See thepatch.ggalign_free_labfunction in thealignpatch-free-lab.Rscript for an example of usage.Value Logical value (
TRUEorFALSE) indicating whetherselfis aPatchAlignpatchesobject.
Examples
patch(ggplot())
#> <ggproto object: Class PatchGgplot, ggalign::Patch, gg>
#> align_border: function
#> align_panel: function
#> border_sizes: function
#> decompose_bg: function
#> decompose_guides: function
#> gtable: function
#> guides: function
#> is_alignpatches: function
#> place: function
#> place_bg: function
#> place_gt: function
#> plot: ggplot2::ggplot, ggplot, ggplot2::gg, S7_object, gg
#> super: <ggproto object: Class PatchGgplot, ggalign::Patch, gg>
