View source: R/ggplot_lister.R
ggplot_lister | R Documentation |
For lectures or presentations, it can often be helpful to show a sequential series of plots.
A common case: show the raw data, show the raw data and the fitted model, then
show the raw data and the fitted model with an overlay of some sort of additional information.
This process can become somewhat tedious for complex buildups, even with the relative streamlining
ggplot provides. This function streamlines that process, and ggplot_list_saver
further streamlines the workflow by also automatically saving the generated figures.
ggplot_lister(gglist, niceties = NULL)
gglist |
A list of ggplot layers, augmented with whole numbers to specify commands. First item in the list must be a |
niceties |
A single ggplot layer or a list of ggplot layers that should be applied to all plots, and
are applied last. Common use case would be a list of axis labels, plot title, and theme. Defaults to NULL; in this case, no layers added.
Note that this formulation is merely for convenience – these layers can also be specified in gglist, but would generally need to be added before the first |
The core functionality comes from the gglist
argument. Layers are added sequentially; whenever
a 0 is encountered in the list, ggplot_lister()
saves the plot up to that point as a new entry in the output list. In some
cases, it can be useful to backtrack ("Okay, but what if we fit the data with a smoothing spline instead of a linear regression?").
Rather than make separate calls to ggplot_lister()
, you can backtrack by adding negative integers to the list.
Adding negative value n
removes the last n
layers in the list before continuing.
For figure sets without backtracking, a simple workflow is to develop the final figure with all
components before using ggplot_lister()
. Then simply copy the ggplot()
call and the entire
collection of layers into a list for the gglist
argument, replacing +
with commas. Move any purely cosmetic
layers (axis labels, themes, etc) to a separate list for niceties
. Then, in the gglist
argument, add new 0
entries to the list after each layer for which you want a new plot.
List of ggplots, in order of creation.
library(tidyverse) dat = data.frame(y = runif(30), x = runif(30)) out = ggplot_lister(gglist = list(ggplot(data = dat, aes(x = x, y = y)), geom_point(), 0, geom_path(),0, -1, geom_smooth(method = 'lm')), niceties = list(xlab("turtle"), ylab("cow"))) out = ggplot_lister(gglist = list(ggplot(data = dat, aes(x = x, y = y)), geom_point(), 0, geom_path(),0, geom_point(aes(x = x+.2), col = 'blue')), niceties = list(xlab("turtle"), ylab("cow")))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.