0. Introduction

We often need special characters in the labels of our figures. These include superscript, subscript, and Greek letters. This takes a little bit of extra work in R and ggplot2.

There are two main ways to handle this: 1) use simple mathematical notation and glue the different parts together and 2) use a lot of unicode and hope your typeface (and system) supports all the characters you want to use.

The output of the figures, as vector PDF or some bitmap, may also need tweaking depending on which of the above two approaches is used.

See also http://docs.ggplot2.org/ since the docs are very good.

library(ggplot2)

1. Superscript, subscript, isotopes in regular labels (labs)

With expression and paste

  • this uses plotmath so need to use {} after the - to act as a character after the minus sign
  • use comma to join different types of text
  • no simple way to add the permil character this way, see below though
  • see plotmath for more details via

demo(plotmath)

The first plot:

qplot(1,1) + labs(x = expression(paste("NO"[3]^-{}, " (mgN/L)")),
                  y = expression(paste(delta^{15}, "N-NO"[3]^-{}, " (vs air)")))

With expression

  • use * to join different types of text
qplot(1,1) + labs(x = expression("NO"[3]^-{}*" (mgN/L)"),
                  y = expression(delta^{15}*"N-NO"[3]^-{}*" (vs air)"))

With Unicode

The spacing of Unicode-based superscripts and subscripts is not always ideal. Not all typefaces will support them. Not all operating systems make it easy to enter them. YMMV.

Greek letters are here https://en.wikibooks.org/wiki/Unicode/Character_reference/0000-0FFF

Permil is here https://en.wikibooks.org/wiki/Unicode/Character_reference/2000-2FFF

The handy part is that the ‰ character can be entered directly or accessed through unicode notation.

U+2083 is ₃, U+207B is ⁻, U+03B4 is δ, U+2030 is ‰

qplot(1,1) + labs(x = "NO₃⁻ (mgN/L)",
                  y = "δ¹⁵N-NO₃⁻ (‰ vs air")

qplot(1,1) + labs(x = "NO\u2083\u207B (mgN/L)",
                  y = "\u3b4\ub9\u2075N-NO\u2083\u207B (\u2030 vs air)")

Good compromise

Using a combination of expression and unicode is probably the easiest compromise

qplot(1,1) + labs(x = expression("NO"[3]^-{}*" (mgN/L)"),
                  y = expression(δ^{15}*"N-NO"[3]^-{}*" (‰ vs air)"))

2. Superscript, subscript, isotopes in facets

Facets in ggplots are a great way to add more information in a clean way to a plot.

Using one of the basic facet examples from http://docs.ggplot2.org/0.9.3.1/facet_grid.html that uses the mtcars dataset, the facet labels can be manually set and be manually set with superscript, subscript and isotope characters

Here plot the mpg (miles per gallon) vs wt (weight) and facet by cyl (numbre of cylinders)

qplot(wt, mpg, data = mtcars) + facet_grid(. ~ cyl)

Then create a new set of labels (cyl2) with superscripts, subscripts, isotopes and maths for use instead of cyl

mtcars$cyl2 <- factor(mtcars$cyl, labels = c("delta^{15}*N-NO[3]^-{}", "NO[3]^-{}", "sqrt(x,y)"))
qplot(wt, mpg, data = mtcars) + facet_grid(. ~ cyl2)

To render these the way we want, we use labeller = label_parsed because we want to parse these labels

mtcars$cyl2 <- factor(mtcars$cyl, labels = c("delta^{15}*N-NO[3]^-{}", "NO[3]^-{}", "sqrt(x,y)"))
qplot(wt, mpg, data = mtcars) + facet_grid(. ~ cyl2,
                                           labeller = label_parsed)

3. Superscript, subscript, isotopes in annotations

Annotations can be added on to the plots by specifying the location and content: http://docs.ggplot2.org/current/annotate.html

Here plot some chemical formulae with annotate, note that “parse = TRUE” is required to render the label content correctly (similar to the way the labeller is required when using facet):

qplot(c(1,50), c(1,50)) +
 annotate("text", x = 15, y = 25, parse = TRUE, label = as.character(expression(δ^{15}*"N-NO"[3]^-{}*" (‰ vs air)"))) +
 annotate("text", x = 20, y = 35, size = 7, parse = TRUE, label = as.character(expression(paste(H[2],PO[4]^"-"))))