(Here’s the “.Rmd” file that produced what you’re reading: label.Rmd.)
Here are several plots with successively harder labels using text, the value of a variable, and plotmath
notation.
curve(sqrt(x), from=0, to=4, main="Ugly plain text: y = sqrt(x)")
plotmath
and bquote
, but no text:curve(sqrt(x), from=0, to=4, main=bquote(sqrt(x)))
plotmath
and text:bquote()
”, the asterisk, “*
”, indicates joiningcurve(sqrt(x), from=0, to=4, main=bquote("The square root of n: " * sqrt(n)))
n = 2
curve(sqrt(x), from=0, to=4, main=paste(sep="", "The square root of ", n, " is ", sqrt(n)))
plotmath
, sqrt(2)
indicates \(\sqrt{2}\)bquote()
facilitates plotmath and also allows the inclusion of the value of a variable:
.()
” indicates, in the context of “bquote()
”, that its argument should be evaluated, so
.(n)
” indicates the value of the “n
” variable, that is, 2.(sqrt(n))
” indicates the value of “sqrt(n)
”, that is, 1.414…bquote()
” combines the previous two elements into a titlen = 2
curve(sqrt(x), from=0, to=4, main=bquote(sqrt(.(n))==.(sqrt(n))))
n = 2
curve(sqrt(x), from=0, to=4, main=bquote("The square root of " * .(n) * " is " * sqrt(.(n))==.(sqrt(n))))