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)

    df <- data.frame(area, leaf_chloro, groups)

    ggplot(df, aes(x = groups, y = area)) + 
      geom_bar(stat = "identity", aes(fill = groups)) + 
      labs(x = "Group", y = expression("Leaf" ~ "area" ~ (cm^2)))

Facet labels:

    df_long <- gather(df, key = var, value = value, -groups)

    df_label <- df_long %>%
      mutate(var_exp = factor(var, 
        levels = c("area", "leaf_chloro"),
        labels = c(
          expression("Leaf" ~ "area" ~ (cm^2)),
          expression("Chlorophyll-" ~ alpha)
        )))

    ggplot(df_label, aes(x = groups, y = value)) + 
      geom_bar(stat = "identity", aes(fill = groups)) +
      facet_wrap(~var_exp, labeller = label_parsed)

Axis labels:

    ggplot(df, aes(x = groups, y = area)) + 
      geom_bar(stat = "identity", aes(fill = groups)) +
      scale_x_discrete(labels = c(
        "A" = expression(alpha),
        "B" = expression(beta), 
        "C" = expression(gamma),
        "D" = expression(epsilon), 
        "E" = expression(omega)))

Custom annotations and labels:

    ggplot(df, aes(x = groups, y = area)) + 
      geom_bar(stat = "identity", aes(fill = groups)) +
      geom_label(aes(x = 1, y = 25, label = "(alpha != Alpha)"), 
        parse = T)