5

I was wondering how I can plot the positive infinity sign and -Infinity sign in plot?

Here is my R code (with no success):

plot(1, ty ='n', ann = F, xlim = c(-4, 6), ylim = c(-3.5, 1.5) )

text(c(-4, 6 ), rep(1, 2), c( bquote(- infinity  ), bquote(infinity  ) ) ) 
zx8754
  • 52,746
  • 12
  • 114
  • 209
rnorouzian
  • 7,397
  • 5
  • 27
  • 72
  • I'm not your downvoter, and found your question interesting, how you tried running two separate `text()`? –  Mar 26 '17 at 20:25
  • @user138773, thanks yes but I wanted to keep my codes short. Thanks anyway. – rnorouzian Mar 26 '17 at 20:29
  • 1
    Per `?plotmath`, use `expression` instead of `bquote`: `text(c(-4, 6 ), rep(1, 2), c( expression(-infinity), expression(infinity) ) ) ` – alistaire Mar 26 '17 at 21:27
  • `text(c(-4, 6 ), rep(1, 2), c( expression(-infinity ), bquote(infinity ) ) )` succeeds. The first item given to `c` must be a true R-expression-classed item. – IRTFM Mar 27 '17 at 00:16

2 Answers2

8

?plotmath starts

If the text argument to one of the text-drawing functions (text, mtext, axis, legend) in R is an expression, the argument is interpreted as a mathematical expression and the output will be formatted according to TeX-like rules.

and the labels parameter of text is documented as

a character vector or expression specifying the text to be written. An attempt is made to coerce other language objects (names and calls) to expressions, and vectors and other classed objects to character vectors by as.character.

(emphasis mine). bquote does not actually return an expression (the R class, not the concept), but a language object (a call, specifically). This is causing two problems:

  • Because R can't handle a vector of calls, c does not actually create a vector, but instead coerces the result to a list, akin to how c(sum, mean) is coerced to a list, and
  • While text would coerce the call returned from bquote itself to an expression (which would get parsed properly), it coerces the list to a character vector, which does not get interpreted according to plotmath.

You could coerce the list of calls produced with c and bquote explicitly with as.expression, but it's quicker to just call expression directly and avoid bquote altogether:

plot(1, ty ='n', ann = F, xlim = c(-4, 6), ylim = c(-3.5, 1.5))

text(c(-4, 6 ), rep(1, 2), expression(-infinity, infinity)) 

plot with infinity symbols

As an end note, c actually does work on multiple expressions—sort of—in that c(expression(-infinity), expression(infinity)) returns expression(-infinity, infinity). Unlike bquote, which has two named parameters, expression takes ... though, so it's easier to just call it once with multiple inputs.

alistaire
  • 42,459
  • 4
  • 77
  • 117
3

Try:

text(c(-4, 6 ), rep(1, 2), c( bquote("- \U221E"), bquote("\U221E") ) ) 
G5W
  • 36,531
  • 10
  • 47
  • 80