Graphics Reference
In-Depth Information
Figure 5-26. Left: plain text; right: math expression
Instead of using a plain text string, it's also possible to enter formulas using R's math expression
syntax, by setting parse=TRUE :
sp + annotate( "text" , label = "r^2 == 0.42" , parse = TRUE
TRUE , x = 16.5 , y = 52 )
Discussion
Text geoms in ggplot2 do not take expression objects directly; instead, they take character strings
that are turned into expressions with parse(text="a + b") .
If you use a math expression, the syntax must be correct for it to be a valid R expression object.
You can test validity by wrapping it in expression() and seeing if it throws an error (make
sure notto use quotes around the expression). In the example here, == is a valid construct in an
expression to express equality, but = is not:
expression(r ^ 2 == 0.42 ) # Valid
expression(r ^ 2 == 0.42 )
expression(r ^ 2 = 0.42 )
# Not valid
Error: unexpected '=' in "expression(r^2 ="
Search WWH ::




Custom Search