## Math Notation for R Plot Titles: expression, bquote, & Greek Letters

In this post you will learn:

1. How to create expressions that have mixed (1) strings, (2) expressions, (3) variables & (4) Greek letters
2. How to pass in values as variables to an expression

I wanted to name this post “Ahhhhhhhhhhh #$@%&!!!!” but SEO isn’t terrific for this title so I tried to make the actual title as Googleable as possible. I’m writing this post for future me and past me. If some of the rest of you find it useful even better. Problem: Math Expressions Specifically: Plotting Them Seems every-time I need to plot a title with math notation I wind up wasting a half an hour on what ought to be an easy task. It’s probably because I don’t have the need to do this task often. It’s also because R has it’s own way to write maths (not LaTeX or something I’m familiar with). It is also because there’s several ways to accomplish this task in R. It’s also because I’ve never spent the time defining how to do the process. I can only control the latter two of these four. Today I define how to write a plot with a title that has a math expression. Success is if I can easily plot the following title: This is a layperson’s guide written for and by a layperson. I’m sure there’s a precise reason for what and why plotting math notations is quirky. I don’t have the cognitive space or care to know why. I’m learning and sharing enough to reliably get the job done. In the future maybe I’ll care more about the why. ## Math Notations Require an Expression or Call This was my first aha. I don’t know exactly what an expression or call is. I also don’t know if math notation can be done without these but since I’m focused on a single way to get this don’t let’s just go with this for now. I just know it’s not a string for sure. Hadley has a whole chapter on expressions if you really want the full treatment read about it here: http://adv-r.had.co.nz/Expressions.html I’ve read it a few times but my brain hasn’t retained the distinctions long-term yet. This stackoverflow question also explains a great bit of the detail between an expression and a call: https://stackoverflow.com/q/20355547/1000343 But let’s get back to the task at hand…a single way to plot the above title with mixed strings, notation, & numbers. ## Use bquote I’ve seen mixed string, number, and math expression annotation done with expression, parse(text=, bquote, substitute & clever use of back ticks. For me, the most straight forward way is bquote. It seems to be pretty flexible for most tasks. Here are the four rules to overcome math notation title blues when using bquote: 1. Strings – Require quotes wrapped w/ tilde separator (e.g., "my text" ~). 2. Math Expressions – Unquoted & follow ?plotmath 3. Numbers – Unquoted when part of math notation 4. Variables – Use .() (pass in string or numeric) Got that? Great! ## Application Now you can build what ever. For example say we want to (1) pass a variable name to a plot title, (2) followed by a math notation (correlation), (3) being equal to a correlation value, (4) followed by a string, and lastly, (5) one more math notation. Well that’s: Use the rules. Here’s a visual representation of the rules. Notice that only a string gets quotes around it? Notice the tilde separators around quoted strings? Notice the cor value is passed in (more on this in a moment)? If you struggle with the math notation see ?plotmath Note that if the correlation being passed in as a variable were just a number manually placed in the expression the value is simply part of the math notation. ## Example #### Code: I’m going to plot this 2 times. One where the variable cor being passed in is a double and then a string (cor2). Notice that the leading zero is used in the double? ## A variable to pass in cor <- -.321 cor2 <- '-.321' par(mfrow = c(1, 2)) plot(1:10, 1:10, main = bquote("Hello" ~ r[xy] == .(cor) ~ "and" ~ B^2)) plot(1:10, 1:10, main = bquote("Hello" ~ r[xy] == .(cor2) ~ "and" ~ B^2))  #### Results in: #### ggplot2: Me To! Works for ggplot2 as well. library(ggplot2) ggplot() + labs(title = bquote("Hello" ~ r[xy] == .(cor2) ~ "and" ~ B^2))  ## Another Way Alas there is more than one way to accomplish math notation in titles in R. If you want one way then bquote and the 4 rules will likely always get it done. Skip this brief section. If you’re still reading…You can also use a parse(text = and paste method. The two approaches are similar to a sprintf vs. paste approach to string manipulation (see paste, paste0, and sprintf). This approach requires a bit more reasoning and the cor2 is coerced to a double with a leading zero when it’s evaluate? There are more ways too but I’ll leave that for the curious reader 🙂 plot(1:10, 1:10, main = parse(text = paste0('"Hello"', ' ~ r[xy] == ', cor2, '~ B^2')))  Hopefully, this post helps me in the future to understand how to format plot titles that contain math notation. I hope it helps you too. Note that this thinking is generalizable to other plot annotations with math notations besides titles. Also, I came across this nicely formatted overview of plotmath and wanted to share: http://vis.supstat.com/2013/04/mathematical-annotation-in-r ## Addendum: Greek Letters As a follow up Julia Silge asked about Greek letters. I hadn’t included them in this post but future me and others will likely be concerned about these puppies as well. I hadn’t tried Greek letters with the bquote approach so I decided to try it with a regression line equation as seen below. Thankfully, bquote seems to work here too. cor2 <- '-.321' plot( 1:10, 1:10, main = bquote("Eq 1:" ~ y[i] == alpha + beta * x[i] + epsilon[i] ~ "or" ~ .(cor2)) ) ## And ggplot2 as well library(ggplot2) ggplot() + labs(title = bquote("Eq 1:" ~ y[i] == alpha + beta * x[i] + epsilon[i] ~ "or" ~ .(cor2)))  Advertisements ## About tylerrinker Data Scientist, open-source developer , #rstats enthusiast, #dataviz geek, and #nlp buff This entry was posted in ggplot2, plot, r, trinker, tylerrinker, visualization, work flow and tagged , , , , , , , , , , . Bookmark the permalink. ### 5 Responses to Math Notation for R Plot Titles: expression, bquote, & Greek Letters 1. billy34 says: You may also consider using package latex2exp to enter formula like labels written using latex notation plot(a, a^2, xlab=TeX(“$\\alpha$”), ylab=TeX(“$\\alpha^2\$”)

2. DanO says:

I bang my head against this problem every few months, thanks for putting together such a clear example!

3. Pingback: Linkdump #71 | WZB Data Science Blog

4. LoSpa says:

Hi Tyler,
Very nice post, thank you.
Could you please tell me (if that is possible of course) how to have a new line in a bquote() expression ?
Many thanks,