I got the symbol in! However for some reason there is a big space
between the >= symbol and the parenthesis. (attached)
g <- g + ggtitle(expression("Tx by Year and Product Type\nAdult
Recipients ("*symbol("\263")*" 18 Years)"))
Also, can I now make this title BOLD?
On 11/12/14, Dennis Murphy <
djm...@gmail.com> wrote:
> Hi:
>
> This is one of those cases where the answer is: it's not as simple as it
> looks.
>
> When you mix text and mathematical notation in R, you need to use
> plotmath; see ?plotmath for an overview. Plotmath contains a number of
> symbols and operators, but it also has support for "non-standard"
> characters in octal or Unicode representations.
>
> When you need to use plotmath, you typically need to create an
> expression. This gets a little trickier in ggplot2 because some
> functions allow you to parse character strings into plotmath
> expressions (e.g., geom_text() and annotate()), whereas in other
> applications you need to pass an expression, such as in axis labels
> and plot titles. Therefore, the first thing you need to add to your
> ggtitle code is the wrapper expression().
>
> Next, plotmath's version of >= is a binary operator, which requires
> something both to the left and right of it. Since you only want the
> right hand side to show, you can stick a phantom character to the left
> of the operator. The following code illustrates this approach.
>
> DF <- data.frame(x = 1:5, y = 1:5)
> library(ggplot2)
>
> ggplot(DF, aes(x, y)) + geom_point() +
> ggtitle(expression("Tx by Yr\nOlder ("*phantom("") >= 18~"Years)"))
>
> Note: the * operator separates the string on the left from that on the
> right; it is useful when you want to connect two distinct parts of the
> expression without a space between them. The operator ~ inserts a
> single space between the two parts to its left and right.
>
> The operator approach is a bit clunky and only works for a single >=,
> so another approach is to insert a symbol into the expression. The
> symbol() function lets you insert a symbol through its octal code. The
>>= symbol has octal code 263 in PDF, so the following is an
> alternative to the above:
>
> ggplot(DF, aes(x, y)) + geom_point() +
> ggtitle(expression("Tx by Yr\nOlder ("*symbol("\263")*" 18 Years)"))
>
> The Unicode representation of the greater-than or equal to symbol is
> U+2265:
>
http://www.fileformat.info/info/unicode/char/2a7e/index.htm
> Both calls below generate an equals sign in my locale:
>
> sessionInfo()$locale
> [1] "English_United States.1252" <snipped for brevity>
>
> #---------
> ggplot(DF, aes(x, y)) + geom_point() +
> ggtitle(expression("Tx by Yr\nOlder ("*symbol("\u2265")*" 18 Years)"))
>
> ggplot(DF, aes(x, y)) + geom_point() +
> ggtitle(expression("Tx by Yr\nOlder (\u2265 18 Years)"))
> #-------
>
> You may or may not have better luck in a different locale. I had the
> same problem when substituting the Unicode character Brandon suggested
> earlier, which is the simplest way to do it
>
> If you're in a locale where Brandon's symbol substitution works, the
> other two will probably work as well, but Brandon's approach is
> simpler and more understandable, particularly for single symbols.
>
> Dennis