Skip to content

Commit 621a348

Browse files
committed
Initial commit
0 parents  commit 621a348

7 files changed

+158
-0
lines changed

.Rbuildignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData

DESCRIPTION

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Package: stickylabeller
2+
Title: Sticky Labeller
3+
Version: 0.0.0.9000
4+
Authors@R: person("James", "Goldie", email = "me@rensa.co", role = c("aut", "cre"))
5+
Description: Create facet labels for ggplot2 using the glue package. Also includes some helpers for sequentially labelling your facets.
6+
Depends: R (>= 3.1)
7+
License: What license is it under?
8+
Encoding: UTF-8
9+
LazyData: true
10+
Imports:
11+
ggplot2,
12+
glue

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Generated by roxygen2: fake comment so roxygen2 overwrites silently.
2+
exportPattern("^[^\\.]")

R/main.r

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
label_glue <- function(template) {
3+
4+
# here's the inner function. label_glue returns this, but it can access the
5+
# template string given when the user calls label_glue
6+
label_glue_inner <- function(labels) {
7+
8+
# TODO - I can check the `facet` attribute to see whether it's a wrap
9+
# or a grid, and the `type` attribute to see rows or columns (direction?)
10+
# facet_type <- attr(labels, "facet")
11+
facet_count <- nrow(labels)
12+
13+
# convert incoming labels to strings and add extra columns
14+
labels = lapply(labels, as.character)
15+
labels[[".n"]] <- as.character(1:facet_count)
16+
labels[[".l"]] <- letters[1:facet_count]
17+
labels[[".L"]] <- toupper(letters[1:facet_count])
18+
19+
# new_labels = glue_data(labels, template)
20+
return(list(unname(glue_data(labels, template))))
21+
}
22+
23+
class(label_glue_inner) <- c("function", "labeller")
24+
return(label_glue_inner)
25+
}

README

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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!

stickylabeller.Rproj

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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

0 commit comments

Comments
 (0)