TITLE: Writing non-standard notation in ggplot2
DATE: 2019-08-20
AUTHOR: John L. Godlee
====================================================================
On the current theme of writing papers, I’ve been preparing figures
in R using ggplot2 for a manuscript. I need to put subscripts, greek
letters and other non-standard bits of text into facet labels and
axis labels, mostly to denote the units of a variable. I found that
there are multiple ways to achieve this and unfortunately it seems
like different ways are necessary in different contexts, however, a
lot of what I needed to do could be covered by expression(), which
constructs mathematical expressions in R using “plotmath” syntax.
I wrote notes on how to accomplish different tasks requiring
plotmath and I thought I would put those notes here. See below:
Non-standard notation in ggplot2
expression() notation:
- Basic arithmetic -
expression(x + y - z %*% a %/% b %+-% e %~~% q != p)
- (x plus y minus z times a divided-by b plus-or-minus e
approximately q does-not-equal p)
- Juxtaposition - expression(paste(x, y, z))
- Subscript - expression("cm"^2), expression("cm"^{x + y-2})
- {} is used to group items together without adding brackets
- Superscript - expression("CO"[2])
- To put a sub and superscript together: R[m]^2
- Degree symbol - expression(32 * degree)
- The * signifies the start of a special character but without
a space before it
- Greek letters - expression(alpha - omega)
- Brackets -
- Fractions - expression(frac('Top', 'Bottom'))
- Roots - expression(sqrt(x, y))
- The y root of x
- Typeface -
expression(plain(x) italic(y) bold(z) bolditalic(a) underline(b)
- Deliberate space - expression(x + phantom(0) + y)
- Normal space - expression("leaf" ~ "area" ~ (cm^2))
Notes:
- expression() can’t take spaces in quoted text,
expression("leaf area" ~ (cm^2)) wouldn’t parse well, but
expression("leaf" ~ "area" ~ (cm^2)) would be fine.
Example workflow for ggplot2 axis titles:
library(ggplot2)
library(dplyr)
library(tidyr)
area <- rnorm(n = 50, mean = 10, sd = 1)
leaf_chloro <- rnorm(n = 50, mean = 100, sd = 2)
groups <- rep(c("A", "B", "C", "D", "E"), times = 10)