| Title: | Decorate a 'ggplot' with Associated Information |
|---|---|
| Description: | 'aplot' supports data-driven composition of a main plot with associated subplots that need precise axis alignment. Unlike general layout-focused tools such as 'cowplot' and 'patchwork', it enables related subplots to be integrated on the top, bottom, left, or right sides of a main plot with matched axes, so that the combined panels can be interpreted as a coherent whole. This design was inspired by the 'Method 2' described in 'ggtree' (G Yu (2018) <doi:10.1093/molbev/msy194>). |
| Authors: | Guangchuang Yu [aut, cre] (ORCID: <https://orcid.org/0000-0002-6485-8781>), Shuangbin Xu [ctb] (ORCID: <https://orcid.org/0000-0003-3513-5362>), Thomas Hackl [ctb] |
| Maintainer: | Guangchuang Yu <[email protected]> |
| License: | Artistic-2.0 |
| Version: | 0.3.0.001 |
| Built: | 2026-06-28 09:04:20 UTC |
| Source: | https://github.com/yulab-smu/aplot |
as.patchwork
as.patchwork(x, align = getOption("aplot_align", default = "xy"))as.patchwork(x, align = getOption("aplot_align", default = "xy"))
x |
object |
align |
"x","y","xy","none", align the axis of x/y or not. |
constructure a 'gglist' object that contains a list of plots ('gglist') and parameters (via '...'), the object can be displayed via the 'plot_list()' function.
gglist(gglist, ...)gglist(gglist, ...)
gglist |
a list of plots |
... |
parameters for plotting the 'gglist' |
gglist object
Guangchuang Yu
title gglistGrob
gglistGrob(x)gglistGrob(x)
x |
A 'gglist' object |
A 'gtable' object
insert an associated plot to left, right, top and bottom of a main plot
insert_left(.data, plot, width = 1) insert_right(.data, plot, width = 1) insert_top(.data, plot, height = 1) insert_bottom(.data, plot, height = 1)insert_left(.data, plot, width = 1) insert_right(.data, plot, width = 1) insert_top(.data, plot, height = 1) insert_bottom(.data, plot, height = 1)
.data |
an 'aplot' or 'gg' object |
plot |
a 'gg' plot to be inserted |
width |
relative width to the main plot |
height |
relative height to the main plot |
The first input serve as a main plot, and other plots can be progressively inserted to different sides on left, right, top and bottom.
an 'aplot' object
Guangchuang Yu
library(ggplot2) library(aplot) p <- ggplot(mtcars, aes(mpg, disp)) + geom_point() p2 <- ggplot(mtcars, aes(mpg)) + geom_density(fill='steelblue', alpha=.5) + ggfun::theme_noxaxis() p3 <- ggplot(mtcars, aes(x=1, y=disp)) + geom_boxplot(fill='firebrick', alpha=.5) + theme_void() ap <- p %>% insert_top(p2, height=.3) %>% insert_right(p3, width=.1) ap ap[2, 1] <- ap[2, 1] + theme_bw() ap[2, 1] <- ap[2, 1] + aes(color = as.factor(am)) + scale_color_manual(values = c('steelblue', 'darkgreen')) ap[1, 1] <- ap[1, 1] + theme(axis.line.x.bottom=element_line()) aplibrary(ggplot2) library(aplot) p <- ggplot(mtcars, aes(mpg, disp)) + geom_point() p2 <- ggplot(mtcars, aes(mpg)) + geom_density(fill='steelblue', alpha=.5) + ggfun::theme_noxaxis() p3 <- ggplot(mtcars, aes(x=1, y=disp)) + geom_boxplot(fill='firebrick', alpha=.5) + theme_void() ap <- p %>% insert_top(p2, height=.3) %>% insert_right(p3, width=.1) ap ap[2, 1] <- ap[2, 1] + theme_bw() ap[2, 1] <- ap[2, 1] + aes(color = as.factor(am)) + scale_color_manual(values = c('steelblue', 'darkgreen')) ap[1, 1] <- ap[1, 1] + theme(axis.line.x.bottom=element_line()) ap
plot a list of ggplot objects using patchwork, similar to 'cowplot::plot_grid(plotlist)'
plot_list( ..., gglist = NULL, ncol = NULL, nrow = NULL, byrow = NULL, widths = NULL, heights = NULL, guides = NULL, labels = NULL, tag_levels = NULL, tag_size = 14, design = NULL, output = "patchwork" )plot_list( ..., gglist = NULL, ncol = NULL, nrow = NULL, byrow = NULL, widths = NULL, heights = NULL, guides = NULL, labels = NULL, tag_levels = NULL, tag_size = 14, design = NULL, output = "patchwork" )
... |
list of plots to be arranged |
gglist |
(optional) list of plots |
ncol |
number of columns |
nrow |
number of rows |
byrow |
If "FALSE" the plots will be filled in in column-major order |
widths |
relative widths |
heights |
relative heights |
guides |
A string specifying how guides should be treated in the layout. |
labels |
manual specified labels to label plots |
tag_levels |
format to label plots, will be disable if 'labels' is not NULL |
tag_size |
size of tags |
design |
specification of the location of areas in the layout |
output |
one of 'gglist' or 'patchwork' |
composite plot
Guangchuang Yu
Set a guide area on an 'aplot' object
set_guide_area( x, position = c("top-right", "top-left", "bottom-right", "bottom-left", "right", "left", "bottom", "top") )set_guide_area( x, position = c("top-right", "top-left", "bottom-right", "bottom-left", "right", "left", "bottom", "top") )
x |
an 'aplot' object |
position |
placement of collected guides. Supported values are '"top-right"', '"top-left"', '"bottom-right"', '"bottom-left"', '"right"', '"left"', '"bottom"', and '"top"'. Corner positions use the largest continuous empty rectangular region anchored at the requested corner. Side positions create a single guide region on the requested side, where the guide box is placed as one object rather than as repeated guide cells. |
Designate a guide placement region in an 'aplot' layout for collected guides. Corner positions use the continuous empty region anchored at the requested corner. Side positions create a guide region on the requested side. The selected guide area remains empty when guides are not collected.
an 'aplot' object
Set collected guide layout on an 'aplot' object
set_guide_layout( x, legend_ncol = NULL, legend_byrow = FALSE, legend_title_position = NULL, legend_direction = NULL, legend_keywidth = NULL, legend_keyheight = NULL, legend_spacing_x = NULL, legend_spacing_y = NULL, guides_ncol = NULL, guides_byrow = FALSE, guides_direction = NULL )set_guide_layout( x, legend_ncol = NULL, legend_byrow = FALSE, legend_title_position = NULL, legend_direction = NULL, legend_keywidth = NULL, legend_keyheight = NULL, legend_spacing_x = NULL, legend_spacing_y = NULL, guides_ncol = NULL, guides_byrow = FALSE, guides_direction = NULL )
x |
an 'aplot' object |
legend_ncol |
number of columns used inside each individual legend. Must be a positive integer. If 'NULL', the internal column layout of each legend is left unchanged. |
legend_byrow |
logical; whether keys inside each individual legend should be filled row-wise when a multi-column legend is created. The default is 'FALSE'. |
legend_title_position |
title position used inside each individual legend. Must be one of '"top"', '"bottom"', '"left"', or '"right"'. If 'NULL', the current title position is left unchanged. |
legend_direction |
direction used inside each individual legend. Must be one of '"horizontal"' or '"vertical"'. If 'NULL', the current legend direction is left unchanged. |
legend_keywidth, legend_keyheight
|
legend key size in millimeters for each individual legend. These values are converted to 'grid::unit(..., "mm")' and passed to [ggplot2::guide_legend()]. If 'NULL', the corresponding key dimension is left unchanged. |
legend_spacing_x, legend_spacing_y
|
spacing between legend items in millimeters inside each individual legend. These values are converted to 'grid::unit(..., "mm")' and applied through [ggplot2::theme()] as 'legend.spacing.x' and 'legend.spacing.y'. If 'NULL', the corresponding spacing is left unchanged. |
guides_ncol |
number of columns used to arrange multiple collected legend boxes as a group. Must be a positive integer. If 'NULL', the group layout stays in a single strip determined by 'guides_direction'. |
guides_byrow |
logical; whether collected legend boxes should be filled row-wise when a wrapped guide group is created. The default is 'FALSE'. |
guides_direction |
overall direction used to arrange multiple collected legend boxes. Must be one of '"horizontal"' or '"vertical"'. If 'NULL', the default follows the guide placement side used by [set_guide_area()]. |
Control collected guides at two semantic levels when an 'aplot' object is rendered. Use [set_guide_area()] to decide where guides should go, and use 'set_guide_layout()' to decide how they should be arranged once placed.
an 'aplot' object
Set panel spacing for an 'aplot' object
set_panel_spacing(x, spacing = 0)set_panel_spacing(x, spacing = 0)
x |
an 'aplot' object |
spacing |
a non-negative numeric value in millimeters, or '"asis"' |
Control the spacing between aligned panels when an 'aplot' object is rendered. Numeric values are interpreted in millimeters and represent the gap between neighboring panels. Use '"asis"' to preserve the subplot margins already defined in each plot.
an 'aplot' object
display x or y axis label as an ordinary text, so that the label will not be aligned with axis label of another plot
xlab2(label, fontsize = 11, ...) ylab2(label, fontsize = 11, ...)xlab2(label, fontsize = 11, ...) ylab2(label, fontsize = 11, ...)
label |
axis label |
fontsize |
fontsize of the label |
... |
additional parameter passed to 'gpar' |
gg object with new label
Guangchuang Yu
set axis limits (x or y) of a 'ggplot' object (left hand side of '+') based on the x ('xlim2') or y ('ylim2') limits of another 'ggplot' object (right hand side of '+'). This is useful for using 'cowplot' or 'patchwork' to align 'ggplot' objects.
xlim2(gg, limits = NULL) ylim2(gg, limits = NULL)xlim2(gg, limits = NULL) ylim2(gg, limits = NULL)
gg |
ggplot object |
limits |
vector of limits. If NULL, determine from 'gg'. |
ggplot2 object with new limits
Guangchuang Yu
library(ggplot2) library(aplot) p1 <- ggplot(mtcars, aes(cyl)) + geom_bar() p2 <- ggplot(subset(mtcars, cyl != 4), aes(cyl)) + geom_bar() p2 + xlim2(p1)library(ggplot2) library(aplot) p1 <- ggplot(mtcars, aes(cyl)) + geom_bar() p2 <- ggplot(subset(mtcars, cyl != 4), aes(cyl)) + geom_bar() p2 + xlim2(p1)