4  ggplot2

4.1 LaTeX in scale

I want to interpret LaTeX formulas contained in a column of a dataset used to plot observations. The easiest way I found to do so is to use the TeX() function from {latex2exp} passed on to the labels argument of the scale_y_discrete() from {ggplot2}.

library(tibble)
library(ggplot2)
df <- tibble(x = c(1,2,3), y = c("Some text $A \\leq 2$", "$A = 2$", "$A \\geq 2$"))
ggplot(data = df, mapping = aes(x = x, y = y)) +
  geom_bar(stat = "identity") +
  scale_y_discrete(labels = function(labels) latex2exp::TeX(labels))