I often want to display both Latex and the underlying value of a variable in a ggplot title, but my existing methods seem to only work for one or the other.
library(tidyverse)
library(glue)
library(latex2exp)
MY_VARIABLE <- 1000
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
labs(title = TeX(r'(Can I use $\LaTeX$ with MY_VARIABLE)')))
MY_VARIABLE <- 1000
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
labs(title = glue('Can I use \\LaTeX with {MY_VARIABLE}'))
Another option using paste0
with TeX
:
library(tidyverse)
library(latex2exp)
MY_VARIABLE <- 1000
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
labs(title = TeX(paste0('Can I use \\LaTeX with', MY_VARIABLE)))
Created on 2022-08-16 by the reprex package (v2.0.1)