Axis Labels Containing Subscripts and Superscripts

The default xlab and ylab do not allow subscripts like r2 or superscripts or xi. For these you need to master the expression function. In R, the operator for superscripts is ‘hat’ (or, more correctly, ‘caret’) so ‘r squared’ is written r^2. Likewise subscripts in R involve square brackets [] so xi is written x[i]. Suppose that we want r2 to be the label on the y axis and xi to be the label on the x axis. The expression function turns R code into text like this:

plot(1:10,1:10, ylab=expression(r^2), xlab=expression(x[i]),type="n")

Get The R Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.