| Title: | Create Overlapping Stacked Plots |
|---|---|
| Description: | Easily create overlapping grammar of graphics plots for scientific data visualization. This style of plotting is particularly common in climatology and oceanography research communities. |
| Authors: | Sebastian Kopf [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-2044-0201>), Tristan Caro [aut] (ORCID: <https://orcid.org/0000-0001-6177-7444>), Jamie McFarlin [aut] (ORCID: <https://orcid.org/0000-0002-8185-8185>), Jon Raberg [aut] (ORCID: <https://orcid.org/0000-0003-2959-3631>) |
| Maintainer: | Sebastian Kopf <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.4.1 |
| Built: | 2026-06-02 08:02:15 UTC |
| Source: | https://github.com/kopflab/ggstackplot |
Easily create overlapping grammar of graphics plots for scientific data visualization. This style of plotting is particularly common in climatology and oceanography research communities.
Have you ever wanted to create (partly) overlapping line plots with matched color-coding of the data and axes? These kinds of plots are common in climatology and oceanography research but there is not an easy way to create them with ggplot facets. The ggstackplot package builds on ggplot2 to provide a straightforward approach to building these kinds of plots while retaining the powerful grammar of graphics approach of ggplots. Check out the functionality provided by ggstackplots at https://ggstackplot.kopflab.org
Maintainer: Sebastian Kopf [email protected] (ORCID) [copyright holder]
Authors:
Tristan Caro [email protected] (ORCID)
Jamie McFarlin [email protected] (ORCID)
Jon Raberg [email protected] (ORCID)
Useful links:
Report bugs at https://github.com/kopflab/ggstackplot/issues
Use ggstackplot() to generate a stackplot. If you need more fine control, use prepare_stackplot() and assemble_stackplot() individually. To explore examples of all the different features, check out the vignette("explore", "ggstackplot") or the online documentation.
ggstackplot( data, x, y, remove_na = TRUE, color = NA, palette = NA, both_axes = FALSE, alternate_axes = TRUE, switch_axes = FALSE, overlap = 0, simplify_shared_axis = TRUE, shared_axis_size = 0.2, template = ggplot() + geom_line() + geom_point() + theme_stackplot(), add = list(), debug = FALSE ) prepare_stackplot( data, x, y, remove_na = TRUE, color = NA, palette = NA, both_axes = FALSE, alternate_axes = TRUE, switch_axes = FALSE, template = ggplot() + geom_line() + geom_point() + theme_stackplot(), add = list(), debug = FALSE ) assemble_stackplot( prepared_stackplot, overlap = 0, simplify_shared_axis = TRUE, shared_axis_size = 0.15, debug = FALSE )ggstackplot( data, x, y, remove_na = TRUE, color = NA, palette = NA, both_axes = FALSE, alternate_axes = TRUE, switch_axes = FALSE, overlap = 0, simplify_shared_axis = TRUE, shared_axis_size = 0.2, template = ggplot() + geom_line() + geom_point() + theme_stackplot(), add = list(), debug = FALSE ) prepare_stackplot( data, x, y, remove_na = TRUE, color = NA, palette = NA, both_axes = FALSE, alternate_axes = TRUE, switch_axes = FALSE, template = ggplot() + geom_line() + geom_point() + theme_stackplot(), add = list(), debug = FALSE ) assemble_stackplot( prepared_stackplot, overlap = 0, simplify_shared_axis = TRUE, shared_axis_size = 0.15, debug = FALSE )
data |
the data frame to plot |
x |
the x variable(s) to plot, accepts |
y |
the y variable(s) to plot, accepts |
remove_na |
whether to remove |
color |
which color to make the plots (also sets the plotwide color and fill aesthetics, overwrite in individual geoms in the |
palette |
which color to make the plots defined with an RColorBrewer palette ( |
both_axes |
whether to have the stacked axes on both sides (overrides alternate_axes and switch_axes) |
alternate_axes |
whether to alternate the sides on which the stacked axes are plotted |
switch_axes |
whether to switch the stacked axes. Not switching means that for vertical stacks the plot at the bottom has the y-axis always on the left side; and for horizontal stacks that the plot on the left has the x-axis on top. Setting |
overlap |
fractional overlap between adjacent plots. The max of 1 means plots are perfectly overlaid. The min of 0 means there is no overlap. If providing multiple values, must be 1 less than the number of stacked plots (since it's describing the overlap/gap between adjacent plots). By default there is no overlap between plots |
simplify_shared_axis |
whether to simplify the shared axis to only be on the last plot (+ first plot if a duplicate secondary axis is set) |
shared_axis_size |
if simplify_shared_axes is true, this determines the size of the shared axis relative to the size of a single plot |
template |
a template plot (ggplot object) to use for the stacked plots |
add |
a list of ggplot component calls to add to specific panel plots, either by panel variable name (named list) or index (unnamed list) |
debug |
|
prepared_stackplot |
a nested data frame, the output from |
ggstackplot() stacks a ggplot template with the provided data and parameters. It returns a plot object generated by cowplot::plot_grid()).
prepare_stackplot() is usually not called directly but can be used to assemble the parts of a stackplot first and then look at them or edit them individually before combining them with assemble_stackplot()]. Returns a nested data frame with all stacked variables (.var), their plot configuration, data, plot object, and theme object.
assemble_stackplot() is usually not called directly but can be used to manually combine a stackplot tibble (typically created by prepare_stockplot()). Returns a plot object generated by cowplot::plot_grid()).
ggstackplot() returns a ggplot with overlayed plot layers
prepare_stackplot() returns a tibble with all plot components
assemble_stackplot() returns a ggplot with overlayed plot layers
# 1 step stackplot (most common use) mtcars |> ggstackplot( x = mpg, y = c(`weight [g]` = wt, qsec, drat, disp), palette = "Set1", overlap = c(1, 0, 0.3) ) # 2 step stackplot mtcars |> prepare_stackplot( x = mpg, y = c(`weight [g]` = wt, qsec, drat, disp), palette = "Set1" ) |> assemble_stackplot(overlap = c(1, 0, 0.3)) # many more examples available in the vignette vignette("ggstackplot")# 1 step stackplot (most common use) mtcars |> ggstackplot( x = mpg, y = c(`weight [g]` = wt, qsec, drat, disp), palette = "Set1", overlap = c(1, 0, 0.3) ) # 2 step stackplot mtcars |> prepare_stackplot( x = mpg, y = c(`weight [g]` = wt, qsec, drat, disp), palette = "Set1" ) |> assemble_stackplot(overlap = c(1, 0, 0.3)) # many more examples available in the vignette vignette("ggstackplot")
Returns a basic ggplot2 theme that extends ggplot2::theme_bw() with a transparent plot background to make sure overlapping plots do not cover each other up.
theme_stackplot()theme_stackplot()
ggplot2::theme() object
library(ggplot2) template <- ggplot() + geom_line() + theme_stackplot() ggstackplot( data = mtcars, x = mpg, y = c(wt, qsec, drat), color = c("#E41A1C", "#377EB8", "#4DAF4A"), template = template )library(ggplot2) template <- ggplot() + geom_line() + theme_stackplot() ggstackplot( data = mtcars, x = mpg, y = c(wt, qsec, drat), color = c("#E41A1C", "#377EB8", "#4DAF4A"), template = template )