|
| 1 | +--- |
| 2 | +title: "Introduction to 'ggtibble'" |
| 3 | +output: rmarkdown::html_vignette |
| 4 | +vignette: > |
| 5 | + %\VignetteIndexEntry{Introduction to 'ggtibble'} |
| 6 | + %\VignetteEngine{knitr::rmarkdown} |
| 7 | + %\VignetteEncoding{UTF-8} |
| 8 | +--- |
| 9 | + |
| 10 | +```{r, include = FALSE} |
| 11 | +knitr::opts_chunk$set( |
| 12 | + collapse = TRUE, |
| 13 | + comment = "#>" |
| 14 | +) |
| 15 | +``` |
| 16 | + |
| 17 | +```{r setup} |
| 18 | +library(ggtibble) |
| 19 | +``` |
| 20 | + |
| 21 | +# Motivation for `ggtibble` |
| 22 | + |
| 23 | +From time to time, having a list of ggplots and being able to work on them like |
| 24 | +a regular ggplot can be very helpful. For example, when writing a report, you |
| 25 | +may want to make a set of figures to separate out various levels of a group, |
| 26 | +then make separate figures for each group. |
| 27 | + |
| 28 | +# Introduction |
| 29 | + |
| 30 | +The `ggtibble` package has two main functions to create sets of figures, |
| 31 | +`ggtibble()` and `gglist()`. These create a tibble with optional labels per |
| 32 | +figure and captions (for `ggtibble()`) or a simpler list of figures (for |
| 33 | +`gglist()`). |
| 34 | + |
| 35 | +Both `ggtibble` and `gglist` objects can have ggplot geoms, facets, labels, and |
| 36 | +lists of those added to them as though they were normal ggplot objects. And, |
| 37 | +you can add a `gglist` to either a `ggtibble` or a `gglist`. |
| 38 | + |
| 39 | +# Typical use |
| 40 | + |
| 41 | +Typical use will load required libraries, setup your plot data, generate the |
| 42 | +plot, and then `knit_print()` it. |
| 43 | + |
| 44 | +When generating the plot: |
| 45 | + |
| 46 | +1. Give your dataset, |
| 47 | +2. Indicate your columns for plotting using the `aes()` mapping as for any ggplot2 object, |
| 48 | +3. Provide the `outercols` which are columns outside your dataset; one plot will |
| 49 | + be generated for each unique level of your data with the `outercols`. Note |
| 50 | + that you cannot use `outercols` columns within the plot, but you will use |
| 51 | + them for captions and labels. |
| 52 | +4. You can give a `caption` with a `glue::glue_data()` specification where valid |
| 53 | + columns are any column names that are in your `outercols` specification. (If |
| 54 | + you don't give a caption, then it will be an empty string, `""`.) |
| 55 | +5. You can give a list of labels which are each processed the same as the |
| 56 | + caption via `glue::glue_data()` and then passed to `labs()`. |
| 57 | +6. After the plot is setup in ways that are specific to `ggtibble`, use it like |
| 58 | + a normal ggplot object adding geoms, etc. |
| 59 | + |
| 60 | +```{r typical, fig.cap=all_plots$caption} |
| 61 | +# Note, add `fig.cap=all_plots$caption` to show the generated caption for the |
| 62 | +# figures |
| 63 | +
|
| 64 | +library(ggtibble) |
| 65 | +library(dplyr) |
| 66 | +library(ggplot2) |
| 67 | +
|
| 68 | +d_plot <- |
| 69 | + mtcars |> |
| 70 | + mutate( |
| 71 | + dispu = "cu. in." |
| 72 | + ) |
| 73 | +all_plots <- |
| 74 | + ggtibble( |
| 75 | + d_plot, |
| 76 | + aes(x = disp, y = hp), |
| 77 | + outercols = c("cyl", "dispu"), |
| 78 | + caption = "Horsepower by displacement for {cyl} cars", |
| 79 | + labs = list(x = "Displacement ({dispu})", y = "Gross horsepower") |
| 80 | + ) + |
| 81 | + geom_point() + |
| 82 | + geom_line() |
| 83 | +
|
| 84 | +# The result is a tibble with columns for the `data_plot`, `figure`, and |
| 85 | +# `caption` |
| 86 | +as_tibble(all_plots) |
| 87 | +
|
| 88 | +# You can then show all the figures with the `knit_print()` method. |
| 89 | +knit_print(all_plots) |
| 90 | +``` |
0 commit comments