Search code examples
rplotmathmathematical-notation

Is it possible to use == twice in R mathematical notation?


I am trying to use R mathematical notation of plotmath in a plot label. In the label, I am trying to use to equality signs, e.g.

ggplot(data=NULL) +
  geom_point(aes(x=0.5, y=0)) +
  geom_text(aes(x=0.5, y=-0.1),
            label = paste("x == frac(3,2) == 1.5"), parse=TRUE) +
  xlim(-1,1) +
  ylim(-1, 1)

However, I obtain the following error:

Error in parse(text = text[[i]]) : <text>:1:16: Unexpected '=='
1: x == frac(3,2) ==
                   ^

Is it possible to use two equality signs within a label?

(This is a follow up question to this question.)


Solution

  • ggplot(NULL) +
      geom_point(aes(x = 0.5, y = 0)) +
      annotate("text", x = 0.5, y = -0.2, label = expression(paste("x = ", frac(3, 2), " = 1.5"))) +
      annotate("text", x = 0.5, y = 0.2,  label = "~x == ~frac(3, 2) == ~1.5", parse = T) +
      annotate("text", x = 0.5, y = -0.4, label = expression({x == frac(3, 2)} == 1.5)) +
      xlim(-1, 1) +
      ylim(-1, 1)