ggplot title MATH symbols

1,483 views
Skip to first unread message

Rachel X

unread,
Nov 17, 2014, 9:53:44 AM11/17/14
to ggp...@googlegroups.com
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
Capture.PNG

Ben Bond-Lamberty

unread,
Nov 17, 2014, 11:58:05 AM11/17/14
to Rachel X, ggplot2
Re bolding, just do expression(bold(...)).

Re the funny spacing, that's weird, but seems to be associated with
using the \n for a line break. I don't have time to test this right
now, but there are some alternate ways of putting line breaks into an
expression here
http://stackoverflow.com/questions/18237134/line-break-in-expression
that it would be worth checking out.

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages