The {xdvir} package provides functions for rendering LaTeX snippets in R plots.
TeX: /usr/bin/latex
xetex: XeTeX 3.14159265-2.6-0.999991 (TeX Live 2019/Debian)
luatex: This is LuaTeX, Version 1.10.0 (TeX Live 2019/Debian)
luaotfload-tool: 3.12
: The luaotfload-tool version is too low (< 3.15).
: The LuaTeX engine is NOT available.
The plot below, produced with R, features a LaTeX-quality mathematical equation in the top-left corner. Producing an equation like this requires three things:
A markup language that allows us to describe the equation.
A typesetter that can select fonts and individual glyphs from within those fonts and determine the exact placement of each individual glyph.
A renderer that can draw the glyphs from the fonts that the typesetter has selected in the locations that the typesetter has determined.
The {xdvir} package allows us to use LaTeX as the markup language, a TeX engine as the typesetter, and R as the renderer. This allows us to produce high-quality LaTeX typesetting within R plots, as in the image above.
The main function in {xdvir} is grid.latex()
, which
takes a LaTeX snippet and draws it on the current R graphics device. For
example, the following code describes the mathematical equation, using
LaTeX code, as a character value, then draws it by calling
grid.latex()
with the character value as the first
argument.
Because LaTeX code tends to contain a large number of backslashes,
the code below uses the r"(...)"
syntax for raw character
constants, so that we do not have to escape each backslash with a double
backslash.
The function grid.latex()
only provides a low-level
interface for drawing LaTeX snippets as part of {grid} output. However,
there are ways to combine {grid} output with higher-level plotting
packages.
For example, we can use {grid} within panel functions in the
{lattice} package (Sarkar 2008). The
following code draws a {lattice} plot and adds the mathematical equation
in the top-left corner. This demonstrates that the
grid.latex()
function has arguments x
,
y
, hjust
, and vjust
for
positioning the LaTeX output.
xyplot(y ~ x, df, type="l",
panel=function(...) {
panel.xyplot(...)
grid.latex(tex,
x=unit(2, "mm"),
y=unit(1, "npc") - unit(2, "mm"),
hjust="left", vjust="top")
})
If we want to add {grid} output to a {graphics} plot, we can use the
{gridGraphics} package (Murrell 2015; Murrell and
Wen 2020). For example, the following code draws a {graphics}
plot, uses gridGraphics::grid.echo()
to convert it to
{grid}, navigates to the {grid} viewport that represents the main plot
region, and draws the mathematical equation in the top-left corner.
plot(y ~ x, df, type="l")
grid.echo()
downViewport("graphics-plot-1")
grid.latex(tex,
x=unit(2, "mm"),
y=unit(1, "npc") - unit(2, "mm"),
hjust="left", vjust="top")
We can add {grid} output to {ggplot2} plots (Wickham 2016) with the {gggrid} package (Murrell 2021, 2022). The following code uses
the latexGrob()
function to generate a “graphical object”
for {ggplot2} to draw, rather than drawing the output immediately as
grid.latex()
does, and passes that to
gggrid::grid_panel()
so that the mathematical equation is
added in the top-left corner of the plot.
gg +
grid_panel(latexGrob(tex,
x=unit(2, "mm"),
y=unit(1, "npc") - unit(2, "mm"),
hjust="left", vjust="top"))
Another way to do this in {ggplot2} is with
ggplot2::annotation_custom()
, though precisely positioning
the annotation may be less convenient.
The {xdvir} package also provides integration with labels in
{ggplot2} plots. This allows, for example, the title of a plot to
contain LaTeX code, as shown in the image below. For this to work, the
relevant theme element must be set using element_latex()
,
as shown in the code below.
Although the need is probably less, for completeness, there is also a
geom_latex()
for adding LaTeX-styled data symbols. For
example, the following code adds LaTeX text labels to a dot plot.
samples <- data.frame(x=rnorm(50), sample=rep(1:5, each=10))
means <- aggregate(samples$x, list(sample=samples$sample), mean)
means$label <- paste0("$\\bar x_", means$sample, "$")
ggplot(samples) +
geom_vline(xintercept=0, linetype="solid", colour=1, linewidth=.5) +
geom_point(aes(x, sample), size=4, alpha=.5) +
geom_point(aes(x, sample), data=means, colour=2, size=4) +
geom_latex(aes(x, sample, label=label), data=means,
size=6, vjust=-.4, colour=2) +
scale_y_continuous(expand=expansion(.25))
The {xdvir} package relies on the glyph rendering support that was
added to the R graphics engine in R version 4.3.0 (Murrell, Pedersen, and Urbanek 2023).
Furthermore, this support is only available on specific graphics
devices: the core pdf()
, Cairo-based devices, and
quartz()
devices, plus the devices provided by the {ragg}
package (Pedersen and Shemanarev 2024). R
Studio users should set the graphics backend to “AGG” or “cairo” (if
either of those is not the default).
The {xdvir} package also relies on there being a TeX installation.
When the package is attached, it reports on the TeX features that it can
find (see the top of this document for an example). If TeX is not found,
the simplest solution is to use
tinytex::install_tinytex()
.