R has powerful graphical capability even in the R base graphics. Here I’ll give a brief tutorial for including mathematical expression in the figures. More details can be found in the help for plotmath, but it is difficult to read and comprehend, especially for beginners. I encourage you to take a look at the example and demo for plotmath first, then read the following.


I assume - you have good understanding for most basic R plotting functions, such as plot, text, etc. - you know the Latex ways to math expressions and symbols.

The focus on this document is to add math equations to the plot.

Quick start

The math formula in R plot needs to be wrapped in an object of expression. You can take a look at the help pages for expression, but when it is used in plot, it means to be rendered as a math formula instead of being evaluated.

Here, I’ll first create an empty figure, and then use the text function to
write a simple expression in the figure.

plot(1:10, 1:10, xlab="", ylab="", type="n")
text(5, 5, expression(f(x)==sqrt(x)))

A few commments on this simple example:

Use expressions in axis labels and figure titles

Similar to the text example above, you can use the expression in axis labels (xlab and ylab parameters) and figure titles (main parameter): Again, these parameters in the plot function usually takes strings. When you give them expressions, they will be shown as math formulas.

x = 1:100
y = sqrt(x)
plot(x, y, xlab="x", ylab=expression(sqrt(x)), main=expression(f(x)==sqrt(x)) )

Make it a little more complicated

Of course, the math formula can be complex, and include Greek letters. This requires you to be familiar with the Latex syntax. For all possible symbols, look at demo(plotmath). The example below, I write the standard normal density function in a figure:

plot(1:10, 1:10, xlab="", ylab="", type="n")
text(5, 5, expression(f[X](x)==frac(1, sigma*sqrt(2*pi))*plain(e)^{frac(-(x-mu)^2, 2*sigma^2)}))

Use expression with paste

Sometimes, you want to mix math expressions with simple text. For that, you have to use paste function to put the expression and text together.

Here is a simple example:

plot(1:10, 1:10, xlab="", ylab="", type="n")
text(5, 5, expression(paste("My function is ", f(x)==sqrt(x))) )

A few notes:

A more complicated example. If you want to display \(f(x; \sigma) = x^\sigma\), Since the greek letter \(\sigma\) need to be typed in as sigma in the expression, you need to paste several components together.

plot(1:10, 1:10, xlab="", ylab="", type="n")
text(5, 5, expression(paste("f(x;", sigma, ") = ", x^sigma)))

In the above expression, I pasted 4 parts together: