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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.