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

131 views Asked by At

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

2

There are 2 answers

1
Merijn van Tilborg On BEST ANSWER
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)
0
Allan Cameron On

You can use paste to join the two statements, though you might need to cheat by having a blank variable name on the left hand side of your second expression.

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

enter image description here