View source: R/ggplot2-integration.R
| geom_latex | R Documentation |
Renders LaTeX math expressions as native grid grobs within a ggplot2 plot. Each label is parsed and laid out by MicroTeX, producing resolution-independent vector output.
geom_latex(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
fontsize = 11,
math_font = "",
lineheight = 1.2,
max_width = 0,
input_mode = c("mixed", "math"),
render_mode = c("typeface", "path"),
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
... |
Other arguments passed to |
fontsize |
Default font size in points. Overridden by the |
math_font |
Name of the math font to use (e.g., |
lineheight |
Multi-line height multiplier (default 1.2), matching
|
max_width |
Maximum width in big points for automatic line wrapping (default: 0, no wrapping). |
input_mode |
How |
render_mode |
Character string: |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
A ggplot2 layer.
geom_latex() understands the following aesthetics (required aesthetics
are in bold):
x
y
label — LaTeX math string
size — font size in points (default: 11)
colour — text colour (default: "black")
angle — rotation angle in degrees (default: 0)
hjust — horizontal justification, 0–1 (default: 0.5)
vjust — vertical justification, 0–1 (default: 0.5)
alpha — transparency (default: 1)
if (requireNamespace("ggplot2", quietly = TRUE)) {
library(ggplot2)
df <- data.frame(
x = 1:3, y = 1:3,
eq = c("x^2", "\\frac{a}{b}", "\\sum_{i=1}^n x_i")
)
ggplot(df, aes(x, y, label = eq)) + geom_latex()
# Use annotate() for single annotations (no legend, no data frame needed)
ggplot(mtcars, aes(wt, mpg)) + geom_point() +
annotate("latex", x = 4, y = 30,
label = r"($\hat{y} = \beta_0 + \beta_1 x$)")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.