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 ) ) )
Notice that the y-values on the graph go from negative infinity ( the lowest point of the graph) and to positive infinity (the top of the graph).
The most interesting thing about infinity is – ∞ < x < ∞, which is the mathematical shorthand for the negative infinity which is less than any real number and the positive infinity which is greater than the real number.
Hold one of the alt keys available right and left side of the shift key. Type 8734 using number pad on your keyboard. This will make the infinity symbol ∞.
?plotmath starts
If the
textargument 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:
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, andtext 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))

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.
Try:
text(c(-4, 6 ), rep(1, 2), c( bquote("- \U221E"), bquote("\U221E") ) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With