I am familiar with using expression()
and bquote()
when plotting special Tex like markup in textual base graphics in R. But how to do "approximately equal to" without having the parser throw an error. I am referring to ?plotmath
when determining the markup.
plot(c(1,100), c(1,100), type="n", axes=FALSE, xlab="", ylab="")
# This works
text(50, 80, expression(paste("The area is 24 ", km^2)))
# This doesn't
text(50, 50, expression(paste("Reginald is ", %~~% , "14 years old")))
This should do the trick:
plot(c(1,100), c(1,100), type="n", axes=FALSE, xlab="", ylab="")
text(50, 50,
expression(paste("Reginald is ", phantom() %~~% phantom(), "14 years old")))
The reason your version didn't work is that the %~~%
'operator' expects to be part of an expression with something to both its left and its right. phantom()
provides you with a something that is (a) invisible and (b) takes up no width --- exactly what you need.
Edit:
I learn something every day. Brian Diggs points out, in the comments, that you can produce the same effect, more simply by just leaving out the commas in the expression (in which case you don't even need a nested call to paste()
). This is the better solution:
text(50, 50, expression("Reginald is " %~~% "14 years old"))