Home > database >  How to evaluate Latex and a variable in a ggplot title?
How to evaluate Latex and a variable in a ggplot title?

Time:08-17

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)'))) 

enter image description here

MY_VARIABLE <- 1000
ggplot(mtcars, aes(x=wt, y=mpg))  
    geom_point()   
    labs(title = glue('Can I use \\LaTeX with {MY_VARIABLE}')) 

enter image description here

CodePudding user response:

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 enter image description here

  • Related