Math Notation for R Plot Titles: expression and bquote

[This article was first published on R – TRinker's R Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In this post you will learn:

  1. How to create expressions that have mixed (1) strings, (2) expressions, & (3) numbers
  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

fustrated_r

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:

Capture

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:

Capture

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

bquote

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.

bquote

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:

Capture

ggplot2: Me To!

Works for ggplot2 as well.

library(ggplot2)

ggplot() + labs(title = bquote("Hello" ~ r[xy] == .(cor2) ~ "and" ~ B^2))

Capture

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')))

Capture

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

To leave a comment for the author, please follow the link and comment on their blog: R – TRinker's R Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)