Getting LaTeX into R Plots

DrewConway picture DrewConway · Sep 8, 2009 · Viewed 91.5k times · Source

I would like to add LaTeX typesetting to elements of plots in R (e.g: the title, axis labels, annotations, etc.) using either the combination of base/lattice or with ggplot2.

Questions:

  • Is there a way to get LaTeX into plots using these packages, and if so, how is it done?
  • If not, are there additional packages needed to accomplish this.

For example, in Python matplotlib compiles LaTeX via the text.usetex packages as discussed here: http://www.scipy.org/Cookbook/Matplotlib/UsingTex

Is there a similar process by which such plots can be generated in R?

Answer

Christopher DuBois picture Christopher DuBois · Sep 12, 2009

Here's an example using ggplot2:

q <- qplot(cty, hwy, data = mpg, colour = displ)
q + xlab(expression(beta +frac(miles, gallon)))

alt text