knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(ggtibble)
ggtibble
From time to time, having a list of ggplots and being able to work on them like a regular ggplot can be very helpful. For example, when writing a report, you may want to make a set of figures to separate out various levels of a group, then make separate figures for each group.
The ggtibble
package has two main functions to create sets of figures,
ggtibble()
and gglist()
. These create a tibble with optional labels per
figure and captions (for ggtibble()
) or a simpler list of figures (for
gglist()
).
Both ggtibble
and gglist
objects can have ggplot geoms, facets, labels, and
lists of those added to them as though they were normal ggplot objects. And,
you can add a gglist
to either a ggtibble
or a gglist
.
Typical use will load required libraries, setup your plot data, generate the
plot, and then knit_print()
it.
When generating the plot:
aes()
mapping as for any ggplot2 object,outercols
which are columns outside your dataset; one plot will
be generated for each unique level of your data with the outercols
. Note
that you cannot use outercols
columns within the plot, but you will use
them for captions and labels.caption
with a glue::glue_data()
specification where valid
columns are any column names that are in your outercols
specification. (If
you don't give a caption, then it will be an empty string, ""
.)glue::glue_data()
and then passed to labs()
.ggtibble
, use it like
a normal ggplot object adding geoms, etc.# Note, add `fig.cap=all_plots$caption` to show the generated caption for the # figures library(ggtibble) library(dplyr) library(ggplot2) d_plot <- mtcars |> mutate( dispu = "cu. in." ) all_plots <- ggtibble( d_plot, aes(x = disp, y = hp), outercols = c("cyl", "dispu"), caption = "Horsepower by displacement for {cyl} cars", labs = list(x = "Displacement ({dispu})", y = "Gross horsepower") ) + geom_point() + geom_line() # The result is a tibble with columns for the `data_plot`, `figure`, and # `caption` as_tibble(all_plots) # You can then show all the figures with the `knit_print()` method. knit_print(all_plots)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.