Title: | Decorate a 'ggplot' with Associated Information |
---|---|
Description: | For many times, we are not just aligning plots as what 'cowplot' and 'patchwork' did. Users would like to align associated information that requires axes to be exactly matched in subplots, e.g. hierarchical clustering with a heatmap. This package provides utilities to aligns associated subplots to a main plot at different sides (left, right, top and bottom) with axes exactly matched. |
Authors: | Guangchuang Yu [aut, cre] , Shuangbin Xu [ctb] , Thomas Hackl [ctb] |
Maintainer: | Guangchuang Yu <[email protected]> |
License: | Artistic-2.0 |
Version: | 0.2.3 |
Built: | 2024-11-06 05:27:41 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()) ap
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()) 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
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)