|
| 1 | +# sticklabeller |
| 2 | + |
| 3 | +The `stickylabeller` package helps you label the facets in your ggplot2 plots. If you know how to use the [`glue`](https://cran.r-project.org/web/packages/glue/index.html) package, you know how to use `stickylabeller`! |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Install `stickylabeller` from GitHub using `devtools`: |
| 8 | + |
| 9 | +```r |
| 10 | +devtools::install_github("rensa/stickylabeller") |
| 11 | +``` |
| 12 | + |
| 13 | +## Use |
| 14 | + |
| 15 | +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 `ggplot2::facet_wrap`: |
| 16 | + |
| 17 | +```r |
| 18 | +library(stickylabeller) |
| 19 | + |
| 20 | +# here's some example data |
| 21 | +mydf = data_frame( |
| 22 | + x = 1:90, |
| 23 | + y = rnorm(90), |
| 24 | + red = rep(letters[1:3], 30), |
| 25 | + blue = c(rep(1, 30), rep(2, 30), rep(3, 30))) |
| 26 | + |
| 27 | +# and here's a plot! |
| 28 | +ggplot(mydf) + |
| 29 | + geom_point(aes(x = x, y = y)) + |
| 30 | + facet_wrap( |
| 31 | + ~ red + blue, |
| 32 | + labeller = label_glue('Red is {red}\nand blue is {blue}')) |
| 33 | + |
| 34 | +``` |
| 35 | + |
| 36 | +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: |
| 37 | + |
| 38 | +```r |
| 39 | +label_glue('Red is {toupper(red)}\nand blue is {blue}') |
| 40 | +``` |
| 41 | + |
| 42 | +### Numbering sequential facets |
| 43 | + |
| 44 | +As well as the columns you include in the facetting specification, `stickylabeller` includes a few helper columns: |
| 45 | + |
| 46 | +- `.n` numbers the facets numerically: `"1"`, `"2"`, `"3"`... |
| 47 | +- `.l` numbers the facets using lowercase letters: `"a"`, `"b"`, `"c"`... |
| 48 | +- `.L` numbers the facets using uppercase letters: `"A"`, `"B"`, `"C"`... |
| 49 | + |
| 50 | +So you can automatically number your facets like: |
| 51 | + |
| 52 | +```r |
| 53 | +label_glue('({.l}) Red is {toupper(red)}\nand blue is {blue}') |
| 54 | +``` |
| 55 | + |
| 56 | +**NOTE:** `.l` and `.L` only currently support up to 26 facets—I haven't yet implemented a way for them to continue with AA, AB, AC, etc. |
| 57 | + |
| 58 | +### Including summary statistics in facet labels |
| 59 | + |
| 60 | +There are a couple of ways to include summary statistics using `stickylabeller`. The most flexible way (but probably not the most performant, if you're working with a _massive_ dataset) is to summarise your data and join it back to the original data, so that the summary statistics appear as new columns in the original data. Then include the summary columns in your facetting specification: |
| 61 | + |
| 62 | +``` |
| 63 | +library(tidyverse) |
| 64 | + |
| 65 | +# summarise the data |
| 66 | +multi_summary = mydf %>% |
| 67 | + group_by(red, blue) %>% |
| 68 | + summarise( |
| 69 | + mean_y = sprintf('%#.2f', mean(y)), |
| 70 | + sd_y = sprintf('%#.2f', sd(y))) %>% |
| 71 | + ungroup() |
| 72 | + |
| 73 | +# join it back to the original data |
| 74 | +mydf = mydf %>% |
| 75 | + inner_join(multi_summary) |
| 76 | + |
| 77 | +# plot! remember to include the summaries in your facetting spec |
| 78 | +ggplot(mydf) + |
| 79 | + geom_point(aes(x = x, y = y)) + |
| 80 | + facet_wrap( |
| 81 | + ~ red + blue + mean_y + sd_y, |
| 82 | + labeller = label_glue( |
| 83 | + '({.L}) Red = {red}, blue = {blue}\n(mean = {mean_y}, SD = {sd_y})')) |
| 84 | + |
| 85 | +``` |
| 86 | +This works even if you're facetting by multiple columns and summarising by multiple columns. Keep in mind, however, that if you're going to continue to work with the data after plotting, you might want to drop the summary columns in order to avoid confusing yourself. |
| 87 | + |
| 88 | +## To-do |
| 89 | + |
| 90 | +- [ ] Make sure this works with `facet_grid` as well; |
| 91 | +- [ ] Pass the named arguments of `glue` on, so that you can use things like temporary variables and custom delimiters |
| 92 | + |
| 93 | +Have fun! |
0 commit comments