May/15

7

# Expressions in R

`expression()` and related functions including `bquote()` are powerful tools for annotating figures with mathematical notation in R. This functionality is not obvious from their respective help files. `demo(plotmath)` nicely shows the huge potential of `expression()`, but does not help that much with getting the code need for many real cases.

I tend to get my expressions to work by trial and lots of errors (although having put this together, I now understand them at least temporarily). I’ve just searched through my code library and extracted and annotated some examples of `expression()` being used. I hope someone finds it useful.

I’m going to use `expression()` with `title()`, but the same expressions can be used with any of the functions (`text()`, `title()`, `mtext()`, `legend()`, etc) used for putting text on plots.

```x11(width=4, height=5, point=14)
par(mar=rep(0,4), cex.main=.8)
plot(1, type="n", axes=FALSE, ann=FALSE)
```

The simplest use of expression is take a character or string of characters and it will be added to the plot. If the string contains spaces, it must be enclosed in quotes (alternatively, the space can be replaced by a tilde `~`, which probably gives better code).

```title(line=-1, main=expression(fish))
```

This use of expression is entirely pointless, but is a useful starting point. Some strings have special meanings, for example infinity will draw the infinity symbol. If for some reason you want to have “infinity” written on your plot, it must be in quotes. Greek letters can be used by giving their name in lower-case or with the first letter capitalised to get the lower or upper case character respectively.

```title(line=-2, main=expression(infinity))
title(line=-3, main=expression(pi))
title(line=-4, main=expression(Delta))
```

Subscript or superscript can be added to a string using ^ and [] notation respectively.

```title(line=-5, main=expression(r^2))
title(line=-6, main=expression(beta))
```

If the string we want to have as sub- or superscript contains a space, the string must be in quotes. Braces can be used to force multiple elements to all be superscript.

Strings can be separated by mathematical operators.

```title(line=-7, main=expression(N[high]-N[low]))
title(line=-8, main=expression(N==5))
```

To make more complicated expressions, build them up from separate parts by either using * or paste to join them together (if you want a multiplication symbol, use `%*%`). The * notation gives nicer code.

```title(line=-9, main=expression(Delta*"R yr"))
title(line=-10, main=expression(paste(Delta,"R yr")))
title(line=-11, main=expression(paste("Two Year Minimum ",O)))
#title(line=-11, main=expression(Two~Year~Minimum~O))
title(line=-12, main=expression(paste("Coefficient ", beta)))
#title(line=-12, main=expression(Coefficient~beta))
title(line=-13, main=expression(paste("TP ", mu,"g l"^-1)))
#title(line=-13, main=expression(TP~mu*g~l^-1))
title(line=-14, main=expression(paste(delta^18,"O")))
#title(line=-14, main=expression(delta^18*O))
title(line=-15, main=expression(paste("Foram ", exp(H*minute[bc]))))
#title(line=-15, main=expression(Foram~exp(H*minute[bc])))
```

To start an `expression()` with a superscript (or subscript), I use an empty string (you can also use `phantom()`).

```title(line=-16, main= expression(""^14*C*" years BP"))
#title(line=-16, main= expression(phantom()^14*C~years~BP))
```

So far so good. But sometimes, you want to use the value of an R-object in plot annotation.

For example, if we wanted to label a point with its x value, this will not work.

```x<-5
title(line=-17, main= expression(x==x))
```

Instead of using `expression()`, we have to use `bquote()`, with the object we want written out inside `.()`

```title(line=-18, main= bquote(x==.(x)))
title(line=-19, main= bquote(x==.(x)~mu*g~l^-1))
```

If you understand these examples, you should be able to use the remainder of the functionality demonstrated by `demo(plotmath)` and at `?plotmath`.

## 1 comment

• Author comment by amyeycott · 14 May 2015 at 11:13

May I suggest a policy for naming posts? That it should include what the thing (eg function) does, as well as the name? Then people searching for ‘how do I do task x’ will pick them up. I have edit privileges but I’m not going to go editing someone’s title, but I wa thinking along the lines of “Expressions: how to get R to make titles or labels with symbols and superscripts in them”. I’m thinking these titles should almost want to be writeable in up-goer five. (http://splasho.com/upgoer5/)