├── .Rbuildignore
├── .gitignore
├── DESCRIPTION
├── LICENSE
├── NAMESPACE
├── NEWS.md
├── R
└── main.r
├── README.md
├── README.qmd
├── man
├── figures
│ ├── README-unnamed-chunk-3-1.png
│ ├── README-unnamed-chunk-4-1.png
│ ├── README-unnamed-chunk-6-1.png
│ ├── README-unnamed-chunk-7-1.png
│ ├── logo.afdesign
│ ├── logo.png
│ └── logo.svg
├── label_glue.Rd
├── make_letters.Rd
└── numbering_present.Rd
└── stickylabeller.Rproj
/.Rbuildignore:
--------------------------------------------------------------------------------
1 | ^.*\.Rproj$
2 | ^\.Rproj\.user$
3 | README.qmd
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .Rproj.user
2 | .Rhistory
3 | .RData
4 | .DS_Store
--------------------------------------------------------------------------------
/DESCRIPTION:
--------------------------------------------------------------------------------
1 | Package: stickylabeller
2 | Title: Quickly Number Ggplot2 Facets
3 | Version: 1.0.0
4 | Authors@R: c(
5 | person("James", "Goldie", email = "me@jamesgoldie.dev", role = c("aut", "cre")),
6 | person("David", "Hugh-Jones", role = "ctb"))
7 | Description: Create facet labels for ggplot2 using the glue package. Also includes some helpers for sequentially labelling your facets.
8 | Depends: R (>= 3.1)
9 | License: MIT + file LICENSE
10 | Encoding: UTF-8
11 | LazyData: true
12 | Imports:
13 | glue,
14 | utils
15 | Suggests:
16 | dplyr,
17 | ggplot2
18 | Roxygen: list(markdown = TRUE)
19 | RoxygenNote: 7.3.2
20 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | YEAR: 2018
2 | COPYRIGHT HOLDER: James Goldie
3 |
--------------------------------------------------------------------------------
/NAMESPACE:
--------------------------------------------------------------------------------
1 | # Generated by roxygen2: do not edit by hand
2 |
3 | export(label_glue)
4 |
--------------------------------------------------------------------------------
/NEWS.md:
--------------------------------------------------------------------------------
1 | # stickylabeller 1.0.0
2 |
3 | * **Breaking change:** the required arguments for [label_glue()] are now `row_template` and `col_template`, not `rows` and `cols`.
4 | - (This may affect users who used the package with [ggplot2::facet_grid()] and passed the templates as named arguments.)
5 | * [label_glue()] now accepts a `summary_data` argument for passing additional summary data to the plot without joining it to the original data.
6 | * [label_glue()] now passes dots onto [glue::glue_data()], allowing its other arguments to be used.
7 |
8 | # stickylabeller 0.0.0.9100
9 |
10 | * Initial release
11 |
--------------------------------------------------------------------------------
/R/main.r:
--------------------------------------------------------------------------------
1 | #' Label facets with a string template.
2 | #'
3 | #' Returns a labeller function that you can give to the `labeller` argument of
4 | #' a `facet_*` function.
5 | #'
6 | #' If you're using [label_glue()] with [ggplot2::facet_wrap()] or you're
7 | #' individually supplying labellers to each variable, you only need one string
8 | #' template: `row_template`.
9 | #'
10 | #' If you're using it with [ggplot2::facet_grid()], you need to supply two
11 | #' templates: one for the rows (`row_template`) and one for the columns
12 | #' (`col_template`).
13 | #'
14 | #' If you're using the labeller with [`ggplot2::facet_wrap()`], you can also
15 | #' use these variables in the templates:
16 | #' * `.n` to add numbers to each facet;
17 | #' * `.l` or `.L` to add lower- or uppercase letters
18 | #' * `.r` or `.R` to add lower- or uppercase roman numerals.
19 | #'
20 | #' @param row_template A string to be used as the template by
21 | #' [glue::glue_data()].
22 | #' @param col_template A string to be used as the template by
23 | #' [glue::glue_data()].
24 | #' @param summary_data A data frame of additional variables to reference in
25 | #' the templates. Must also include the facet grouping variables.
26 | #' @param ... Other arguments to be passed to [`glue::glue_data()`]
27 | #'
28 | #' @return A labelling function that you can give to the `labeller` argument
29 | #' of the facetting function.
30 | #' @examples
31 | #' library(ggplot2)
32 | #' library(stickylabeller)
33 | #'
34 | #' # wrap facet columns in braces to refer to their values in the labels
35 | #' p1 <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) + geom_point()
36 | #' p1 + facet_wrap(
37 | #' ~ Species,
38 | #' labeller = label_glue("Sepal and petal lengths in {Species} plants"))
39 | #'
40 | #' # distinguish panels with .n (numbers), .l (lowercase), .L (uppercase),
41 | #' # .r or .R (lower- or uppercase roman) if you're using facet_wrap
42 | #' p1 + facet_wrap(
43 | #' ~ Species,
44 | #' labeller = label_glue("({.n}) {Species}"))
45 | #'
46 | #' # you can also use label_glue with facet_grid
47 | #' p2 <- ggplot(mtcars, aes(x = disp, y = mpg)) + geom_point()
48 | #' p2 + facet_grid(
49 | #' gear ~ cyl,
50 | #' labeller = label_glue(
51 | #' row_template = "{gear} gears",
52 | #' col_template = "{cyl} cylinders"))
53 | #'
54 | #' # you can add summary statistics in a couple of ways. the easiest (in terms
55 | #' # of plot code) is to join a summary back into the original data and to add
56 | #' # the new columns in the facet spec
57 | #' library(dplyr)
58 | #' cyl_stats <- mtcars %>%
59 | #' group_by(cyl) %>%
60 | #' summarise(cyl_n = n(), cyl_meanmpg = sprintf("%#.2f", mean(mpg)))
61 | #' mtcars_joined <- mtcars %>% inner_join(cyl_stats)
62 | #'
63 | #' p3 <- ggplot(mtcars_joined, aes(x = disp, y = mpg)) + geom_point()
64 | #' p3 + facet_wrap(
65 | #' ~ cyl + cyl_n + cyl_meanmpg,
66 | #' labeller = label_glue(
67 | #' "({.l}) {cyl} cylinders\n(n = {cyl_n}, mean = {cyl_meanmpg})"))
68 | #'
69 | #'
70 | #' @export
71 | label_glue <- function(row_template, col_template, summary_data = NULL, ...) {
72 |
73 | # here's the inner function. label_glue returns this, but it can access the
74 | # template string given when the user calls label_glue
75 | label_glue_inner <- function(labels) {
76 |
77 | # grab facet info before a possible merge with summary data nukes it
78 | facet_type <- attr(labels, "facet")
79 | facet_direction <- attr(labels, "type")
80 |
81 | # merge summary_data if it exists
82 | if (is.data.frame(summary_data)) {
83 | labels <- merge(labels, summary_data)
84 | }
85 |
86 | if (!is.null(facet_type) & facet_type == "wrap") {
87 |
88 | # for facet_wrap, convert incoming labels to strings
89 | # and add extra columns for numbering
90 | facet_count <- nrow(labels)
91 | template <- row_template
92 | labels <- lapply(labels, as.character)
93 | labels[[".n"]] <- as.character(seq_len(facet_count))
94 | labels[[".l"]] <- make_letters(seq_len(facet_count))
95 | labels[[".L"]] <- toupper(make_letters(seq_len(facet_count)))
96 | labels[[".r"]] <- tolower(as.character(utils::as.roman(1:facet_count)))
97 | labels[[".R"]] <- as.character(utils::as.roman(1:facet_count))
98 |
99 | } else if (!is.null(facet_type) & facet_type == "grid") {
100 |
101 | if (numbering_present(col_template) | numbering_present(row_template)) {
102 | stop(paste("Error: the column or row label contains .n, .l or .L.",
103 | "label_glue can currently only number the facets in facet_wrap.",
104 | "For more info, see",
105 | "https://github.com/jimjam-slam/stickylabeller/issues/1"))
106 | }
107 |
108 | if (facet_direction == "rows") {
109 | template <- row_template
110 | } else if (facet_direction == "cols") {
111 | template <- col_template
112 | } else {
113 | stop(paste("Error: unrecognised facet_direction in label_guide. This",
114 | "is probably a bug in stickylabeller. Please report it to",
115 | "https://github.com/jimjam-slam/stickylabeller/issues"))
116 | }
117 | labels <- lapply(labels, as.character)
118 |
119 | } else {
120 |
121 | # if no facet type is specified (eg. inside labeller wrapper), just do
122 | # the basics
123 |
124 | if (numbering_present(row_template)) {
125 | stop(paste("Error: the column or row label contains .n, .l or .L.",
126 | "label_glue can currently only number the facets in facet_wrap.",
127 | "For more info, see",
128 | "https://github.com/jimjam-slam/stickylabeller/issues/1"))
129 | }
130 | template <- row_template
131 | labels <- lapply(labels, as.character)
132 | }
133 |
134 | return(list(unname(glue::glue_data(labels, template, ...))))
135 | }
136 |
137 | class(label_glue_inner) <- c("function", "labeller")
138 | return(label_glue_inner)
139 | }
140 |
141 | # helper functions ============================================================
142 |
143 | #' Detect the use of numbering variables
144 | #'
145 | #' Numbering variables are only supported in string templates when using
146 | #' [ggplot2::facet_wrap()].
147 | #' @param template The string template to check
148 | #' @return A boolean: TRUE if a numbering variable is present.
149 | numbering_present <- function(template) {
150 | # TODO - need a more sophisticated regex that can catch more complex
151 | # expressions including numbering columns
152 | return(grepl("{[\\s\\W]*\\.[nlLrR](?!\\w)[\\s\\W]*}", template, perl = TRUE))
153 | }
154 |
155 | #' Build a sequence of letters to extend past 26 facets, in the style of
156 | #' spreadsheet column indices (Y, Z, AA, AB, AC, ...).
157 | #'
158 | #' (Adapted from cellranger::letter_to_num)
159 | #' @param y A vector of indices to create letters for
160 | #' @return A vector of letters potentially extending past a–z
161 | make_letters <- function(y) {
162 | jfun <- function(div) {
163 | if (is.na(div)) return(NA_character_)
164 | ret <- integer()
165 | while (div > 0) {
166 | remainder <- ((div - 1) %% 26) + 1
167 | ret <- c(remainder, ret)
168 | div <- (div - remainder) %/% 26
169 | }
170 | paste(letters[ret], collapse = "")
171 | }
172 | ret <- vapply(y, jfun, character(1))
173 | if (length(ret) == 0) return(ret) # vapply doesn't always work
174 | ifelse(ret == "", NA_character_, ret)
175 | }
176 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | # stickylabeller
6 |
7 | [](https://jimjam-slam.r-universe.dev/stickylabeller)
8 |
9 | The `stickylabeller` package helps you label the facets in your ggplot2
10 | plots using
11 | [`glue`](https://cran.r-project.org/web/packages/glue/index.html).
12 |
13 | ## Installation
14 |
15 | Install `stickylabeller` from my R-Universe:
16 |
17 | ``` r
18 | install.packages("stickylabeller", repos = c(
19 | "https://jimjam-slam.r-universe.dev",
20 | "https://cloud.r-project.org"))
21 | ```
22 |
23 | ## Use
24 |
25 | The package has just one function: `label_glue`. Give it a string
26 | template to be processed by `glue`, and it’ll return a labelling
27 | function that you can pass to `facet_*`:
28 |
29 | ``` r
30 | library(ggplot2)
31 | library(stickylabeller)
32 |
33 | # here's some example data: some random points in groups
34 | mydf <- data.frame(
35 | x = 1:90,
36 | y = rnorm(90),
37 | red = rep(letters[1:3], 30),
38 | blue = c(rep(1, 30), rep(2, 30), rep(3, 30)))
39 |
40 | # and here's a labelled plot!
41 | ggplot(mydf) +
42 | geom_point(aes(x = x, y = y)) +
43 | facet_wrap(
44 | vars(red, blue),
45 | labeller = label_glue("Red is {red}\nand blue is {blue}"))
46 | ```
47 |
48 |
50 |
51 | ``` r
52 | ggplot(mydf) +
53 | geom_point(aes(x = x, y = y)) +
54 | facet_grid(
55 | vars(red), vars(blue),
56 | labeller = label_glue(
57 | "Red is {red}",
58 | "Blue is {blue}"))
59 | ```
60 |
61 |
63 |
64 | Your `label_glue` labeller can refer to any of the data frame columns
65 | included in the facetting formula. It can also use those columns in
66 | expressions, like:
67 |
68 | ``` r
69 | my_labeller <- label_glue("Red is {toupper(red)}\nand blue is {blue}")
70 | ```
71 |
72 | ### Numbering sequential facets
73 |
74 | As well as the columns you include in the facetting specification,
75 | `stickylabeller` includes a few helper columns:
76 |
77 | - `.n` numbers the facets numerically: `1`, `2`, `3`…
78 | - `.l` numbers the facets using lowercase letters: `a`, `b`, `c`…
79 | - `.L` numbers the facets using uppercase letters: `A`, `B`, `C`…
80 | - `.r` numbers the facets using lowercase Roman numerals: `i`, `ii`,
81 | `iii`…
82 | - `.R` numbers the facets using uppercase Roman numerals: `I`, `II`,
83 | `III`…
84 |
85 | So you can automatically number your facets like:
86 |
87 | ``` r
88 | ggplot(mydf) +
89 | geom_point(aes(x = x, y = y)) +
90 | facet_wrap(
91 | vars(red, blue),
92 | labeller = label_glue("({.l}) Red is {toupper(red)}\nand blue is {blue}"))
93 | ```
94 |
95 |
97 |
98 | #### Limitations
99 |
100 | - `.n`, `.l` and `.L` only work with `facet_wrap` for now. See [Issue
101 | \#1](https://github.com/jimjam-slam/stickylabeller/issues/1).
102 |
103 | ### Including summary statistics in facet labels
104 |
105 | As of stickylabeller 1.0, `label_glue` now accepts a `summary_data`
106 | argument. This accepts a data frame of pre-computed summary data for the
107 | facets; it should include the facet grouping columns, plus any other
108 | variables you’d like to reference in the templates.
109 |
110 | For example:
111 |
112 | ``` r
113 | library(magrittr)
114 | library(dplyr)
115 | #>
116 | #> Attaching package: 'dplyr'
117 | #> The following objects are masked from 'package:stats':
118 | #>
119 | #> filter, lag
120 | #> The following objects are masked from 'package:base':
121 | #>
122 | #> intersect, setdiff, setequal, union
123 |
124 | # summarise the data
125 | mydf_stats = mydf |>
126 | group_by(red, blue) |>
127 | summarise(
128 | mean_y = sprintf("%#.2f", mean(y)),
129 | sd_y = sprintf("%#.2f", sd(y))) |>
130 | ungroup()
131 | #> `summarise()` has grouped output by 'red'. You can override using the `.groups`
132 | #> argument.
133 |
134 | # pass the summary data onto label_glue
135 | ggplot(mydf) +
136 | geom_point(aes(x = x, y = y)) +
137 | facet_wrap(
138 | vars(red, blue),
139 | labeller = label_glue(
140 | "({.L}) Red = {red}, blue = {blue}\n(mean = {mean_y}, SD = {sd_y})",
141 | summary_data = mydf_stats))
142 | ```
143 |
144 |
146 |
147 | Have fun! If you hit any snags, please feel free to [file an issue
148 | here](https://github.com/jimjam-slam/stickylabeller/issues) so that I
149 | can get on it! \<3
150 |
--------------------------------------------------------------------------------
/README.qmd:
--------------------------------------------------------------------------------
1 | ---
2 | format: gfm
3 | ---
4 |
5 |
6 |
7 | ```{r}
8 | #| include: false
9 | knitr::opts_chunk$set(
10 | collapse = TRUE,
11 | comment = "#>",
12 | fig.path = "man/figures/README-",
13 | out.width = "100%"
14 | )
15 | set.seed(1)
16 | ```
17 |
18 | # stickylabeller
19 |
20 | [](https://jimjam-slam.r-universe.dev/stickylabeller)
21 |
22 | The `stickylabeller` package helps you label the facets in your ggplot2 plots using [`glue`](https://cran.r-project.org/web/packages/glue/index.html).
23 |
24 | ## Installation
25 |
26 | Install `stickylabeller` from my R-Universe:
27 |
28 | ```{r}
29 | #| eval: false
30 | install.packages("stickylabeller", repos = c(
31 | "https://jimjam-slam.r-universe.dev",
32 | "https://cloud.r-project.org"))
33 | ```
34 |
35 | ## Use
36 |
37 | The package has just one function: `label_glue`. Give it a string template to be processed by `glue`, and it'll return a labelling function that you can pass to `facet_*`:
38 |
39 | ```{r}
40 | #| fig-cap: "facet_wrap labelled with two facet column values"
41 |
42 | library(ggplot2)
43 | library(stickylabeller)
44 |
45 | # here's some example data: some random points in groups
46 | mydf <- data.frame(
47 | x = 1:90,
48 | y = rnorm(90),
49 | red = rep(letters[1:3], 30),
50 | blue = c(rep(1, 30), rep(2, 30), rep(3, 30)))
51 |
52 | # and here's a labelled plot!
53 | ggplot(mydf) +
54 | geom_point(aes(x = x, y = y)) +
55 | facet_wrap(
56 | vars(red, blue),
57 | labeller = label_glue("Red is {red}\nand blue is {blue}"))
58 | ```
59 |
60 | ```{r}
61 | #| fig-cap: "facet_grid labelled with one facet column value on each margin"
62 |
63 | ggplot(mydf) +
64 | geom_point(aes(x = x, y = y)) +
65 | facet_grid(
66 | vars(red), vars(blue),
67 | labeller = label_glue(
68 | "Red is {red}",
69 | "Blue is {blue}"))
70 | ```
71 |
72 | Your `label_glue` labeller can refer to any of the data frame columns included in the facetting formula. It can also use those columns in expressions, like:
73 |
74 | ```{r}
75 | my_labeller <- label_glue("Red is {toupper(red)}\nand blue is {blue}")
76 | ```
77 |
78 | ### Numbering sequential facets
79 |
80 | As well as the columns you include in the facetting specification, `stickylabeller` includes a few helper columns:
81 |
82 | - `.n` numbers the facets numerically: `1`, `2`, `3`...
83 | - `.l` numbers the facets using lowercase letters: `a`, `b`, `c`...
84 | - `.L` numbers the facets using uppercase letters: `A`, `B`, `C`...
85 | - `.r` numbers the facets using lowercase Roman numerals: `i`, `ii`, `iii`...
86 | - `.R` numbers the facets using uppercase Roman numerals: `I`, `II`, `III`...
87 |
88 | So you can automatically number your facets like:
89 |
90 | ```{r}
91 | #| fig-cap: "facet_wrap labelled with sequential lowercase letters: (a), (b), (c) and so on"
92 |
93 | ggplot(mydf) +
94 | geom_point(aes(x = x, y = y)) +
95 | facet_wrap(
96 | vars(red, blue),
97 | labeller = label_glue("({.l}) Red is {toupper(red)}\nand blue is {blue}"))
98 | ```
99 |
100 | #### Limitations
101 |
102 | * `.n`, `.l` and `.L` only work with `facet_wrap` for now. See [Issue #1](https://github.com/jimjam-slam/stickylabeller/issues/1).
103 |
104 | ### Including summary statistics in facet labels
105 |
106 | As of stickylabeller 1.0, `label_glue` now accepts a `summary_data` argument. This accepts a data frame of pre-computed summary data for the facets; it should include the facet grouping columns, plus any other variables you'd like to reference in the templates.
107 |
108 | For example:
109 |
110 | ```{r}
111 | #| fig-cap: "facet_wrap labelled with two facet column values and two summary statistics, each numbered (a), (b), (c) and so on"
112 | library(magrittr)
113 | library(dplyr)
114 |
115 | # summarise the data
116 | mydf_stats = mydf |>
117 | group_by(red, blue) |>
118 | summarise(
119 | mean_y = sprintf("%#.2f", mean(y)),
120 | sd_y = sprintf("%#.2f", sd(y))) |>
121 | ungroup()
122 |
123 | # pass the summary data onto label_glue
124 | ggplot(mydf) +
125 | geom_point(aes(x = x, y = y)) +
126 | facet_wrap(
127 | vars(red, blue),
128 | labeller = label_glue(
129 | "({.L}) Red = {red}, blue = {blue}\n(mean = {mean_y}, SD = {sd_y})",
130 | summary_data = mydf_stats))
131 |
132 | ```
133 |
134 | Have fun! If you hit any snags, please feel free to [file an issue here](https://github.com/jimjam-slam/stickylabeller/issues) so that I can get on it! <3
135 |
--------------------------------------------------------------------------------
/man/figures/README-unnamed-chunk-3-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jimjam-slam/stickylabeller/6027f86e9f95b37e716870481c3bdce60b1991a4/man/figures/README-unnamed-chunk-3-1.png
--------------------------------------------------------------------------------
/man/figures/README-unnamed-chunk-4-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jimjam-slam/stickylabeller/6027f86e9f95b37e716870481c3bdce60b1991a4/man/figures/README-unnamed-chunk-4-1.png
--------------------------------------------------------------------------------
/man/figures/README-unnamed-chunk-6-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jimjam-slam/stickylabeller/6027f86e9f95b37e716870481c3bdce60b1991a4/man/figures/README-unnamed-chunk-6-1.png
--------------------------------------------------------------------------------
/man/figures/README-unnamed-chunk-7-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jimjam-slam/stickylabeller/6027f86e9f95b37e716870481c3bdce60b1991a4/man/figures/README-unnamed-chunk-7-1.png
--------------------------------------------------------------------------------
/man/figures/logo.afdesign:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jimjam-slam/stickylabeller/6027f86e9f95b37e716870481c3bdce60b1991a4/man/figures/logo.afdesign
--------------------------------------------------------------------------------
/man/figures/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jimjam-slam/stickylabeller/6027f86e9f95b37e716870481c3bdce60b1991a4/man/figures/logo.png
--------------------------------------------------------------------------------
/man/figures/logo.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
281 |
--------------------------------------------------------------------------------
/man/label_glue.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/main.r
3 | \name{label_glue}
4 | \alias{label_glue}
5 | \title{Label facets with a string template.}
6 | \usage{
7 | label_glue(row_template, col_template, summary_data = NULL, ...)
8 | }
9 | \arguments{
10 | \item{row_template}{A string to be used as the template by
11 | \code{\link[glue:glue]{glue::glue_data()}}.}
12 |
13 | \item{col_template}{A string to be used as the template by
14 | \code{\link[glue:glue]{glue::glue_data()}}.}
15 |
16 | \item{summary_data}{A data frame of additional variables to reference in
17 | the templates. Must also include the facet grouping variables.}
18 |
19 | \item{...}{Other arguments to be passed to \code{\link[glue:glue]{glue::glue_data()}}}
20 | }
21 | \value{
22 | A labelling function that you can give to the \code{labeller} argument
23 | of the facetting function.
24 | }
25 | \description{
26 | Returns a labeller function that you can give to the \code{labeller} argument of
27 | a \verb{facet_*} function.
28 | }
29 | \details{
30 | If you're using \code{\link[=label_glue]{label_glue()}} with \code{\link[ggplot2:facet_wrap]{ggplot2::facet_wrap()}} or you're
31 | individually supplying labellers to each variable, you only need one string
32 | template: \code{row_template}.
33 |
34 | If you're using it with \code{\link[ggplot2:facet_grid]{ggplot2::facet_grid()}}, you need to supply two
35 | templates: one for the rows (\code{row_template}) and one for the columns
36 | (\code{col_template}).
37 |
38 | If you're using the labeller with \code{\link[ggplot2:facet_wrap]{ggplot2::facet_wrap()}}, you can also
39 | use these variables in the templates:
40 | \itemize{
41 | \item \code{.n} to add numbers to each facet;
42 | \item \code{.l} or \code{.L} to add lower- or uppercase letters
43 | \item \code{.r} or \code{.R} to add lower- or uppercase roman numerals.
44 | }
45 | }
46 | \examples{
47 | library(ggplot2)
48 | library(stickylabeller)
49 |
50 | # wrap facet columns in braces to refer to their values in the labels
51 | p1 <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) + geom_point()
52 | p1 + facet_wrap(
53 | ~ Species,
54 | labeller = label_glue("Sepal and petal lengths in {Species} plants"))
55 |
56 | # distinguish panels with .n (numbers), .l (lowercase), .L (uppercase),
57 | # .r or .R (lower- or uppercase roman) if you're using facet_wrap
58 | p1 + facet_wrap(
59 | ~ Species,
60 | labeller = label_glue("({.n}) {Species}"))
61 |
62 | # you can also use label_glue with facet_grid
63 | p2 <- ggplot(mtcars, aes(x = disp, y = mpg)) + geom_point()
64 | p2 + facet_grid(
65 | gear ~ cyl,
66 | labeller = label_glue(
67 | row_template = "{gear} gears",
68 | col_template = "{cyl} cylinders"))
69 |
70 | # you can add summary statistics in a couple of ways. the easiest (in terms
71 | # of plot code) is to join a summary back into the original data and to add
72 | # the new columns in the facet spec
73 | library(dplyr)
74 | cyl_stats <- mtcars \%>\%
75 | group_by(cyl) \%>\%
76 | summarise(cyl_n = n(), cyl_meanmpg = sprintf("\%#.2f", mean(mpg)))
77 | mtcars_joined <- mtcars \%>\% inner_join(cyl_stats)
78 |
79 | p3 <- ggplot(mtcars_joined, aes(x = disp, y = mpg)) + geom_point()
80 | p3 + facet_wrap(
81 | ~ cyl + cyl_n + cyl_meanmpg,
82 | labeller = label_glue(
83 | "({.l}) {cyl} cylinders\n(n = {cyl_n}, mean = {cyl_meanmpg})"))
84 |
85 |
86 | }
87 |
--------------------------------------------------------------------------------
/man/make_letters.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/main.r
3 | \name{make_letters}
4 | \alias{make_letters}
5 | \title{Build a sequence of letters to extend past 26 facets, in the style of
6 | spreadsheet column indices (Y, Z, AA, AB, AC, ...).}
7 | \usage{
8 | make_letters(y)
9 | }
10 | \arguments{
11 | \item{y}{A vector of indices to create letters for}
12 | }
13 | \value{
14 | A vector of letters potentially extending past a–z
15 | }
16 | \description{
17 | (Adapted from cellranger::letter_to_num)
18 | }
19 |
--------------------------------------------------------------------------------
/man/numbering_present.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/main.r
3 | \name{numbering_present}
4 | \alias{numbering_present}
5 | \title{Detect the use of numbering variables}
6 | \usage{
7 | numbering_present(template)
8 | }
9 | \arguments{
10 | \item{template}{The string template to check}
11 | }
12 | \value{
13 | A boolean: TRUE if a numbering variable is present.
14 | }
15 | \description{
16 | Numbering variables are only supported in string templates when using
17 | \code{\link[ggplot2:facet_wrap]{ggplot2::facet_wrap()}}.
18 | }
19 |
--------------------------------------------------------------------------------
/stickylabeller.Rproj:
--------------------------------------------------------------------------------
1 | Version: 1.0
2 |
3 | RestoreWorkspace: No
4 | SaveWorkspace: No
5 | AlwaysSaveHistory: Default
6 |
7 | EnableCodeIndexing: Yes
8 | UseSpacesForTab: Yes
9 | NumSpacesForTab: 2
10 | Encoding: UTF-8
11 |
12 | RnwWeave: Sweave
13 | LaTeX: pdfLaTeX
14 |
15 | AutoAppendNewline: Yes
16 | StripTrailingWhitespace: Yes
17 |
18 | BuildType: Package
19 | PackageUseDevtools: Yes
20 | PackageInstallArgs: --no-multiarch --with-keep.source
21 | PackageRoxygenize: rd,collate,namespace
22 |
--------------------------------------------------------------------------------