0

My goal is to annotate a plot made with ggplot2 by placing an equation in white space within the plot using the package latex2exp

For example, given the equation:

eqn <- "$\\textbf{Volume}(ml) = 0.035 \\cdot \\textbf{CSA}(mm^2) + 0.127 \\cdot \\textbf{age}(months) - 7.8$"

Running the following code in R 3.5:

library(ggplot2)
library(latex2exp)

ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() + 
  annotate("text", x = 4, y = 40, label=TeX(eqn), hjust=0, size = 5)

My example code fails with the error below:

Error in stats::complete.cases(df[, vars, drop = FALSE]) : 
  invalid 'type' (expression) of argument

What am I doing wrong?

user25494
  • 1,289
  • 14
  • 27

1 Answers1

1

A working solution, based on:

https://github.com/stefano-meschiari/latex2exp/issues/13

ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() + 
  annotate("text", x=3, y=40, label=TeX(eqn, output="character"),
           hjust=0, size = 4, parse = TRUE)

enter image description here

user25494
  • 1,289
  • 14
  • 27