1

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

Lukas D. Sauer
  • 355
  • 2
  • 11
  • 1
    Yes, but you need to break your logical expression into two sub-expressions. Try `x == frac(3,2) & x == 1.5` or similar. – Limey Jan 31 '22 at 11:33
  • I'd call that a no, but I think the point gets across – shs Jan 31 '22 at 11:46
  • Thank you very much. @Limey, note that `==` is parsed into a mathematical equality sign = in plotmath notation. Your suggestion does not solve my problem. @shs, I am not sure whether I understand you correctly. Do mean that it is impossible to achieve what I am asking for? – Lukas D. Sauer Jan 31 '22 at 12:04

2 Answers2

3
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)
Merijn van Tilborg
  • 5,452
  • 1
  • 7
  • 22
3

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

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87