<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/math-notation-for-r-plot-titles-expression-and-bquote/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://trinkerrstuff.wordpress.com/2018/03/15/2246/"> R – TRinker's R Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this post you will learn:</p>
<ol>
<li><strong>How to create expressions that have mixed (1) strings, (2) expressions, &amp; (3) numbers</strong></li>
<li><strong>How to pass in values as variables to an expression</strong></li>
</ol>
<p>I wanted to name this post “Ahhhhhhhhhhh #$@%&amp;!!!!” 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.</p>
<p><strong>Problem:</strong> Math Expressions<br>
<strong>Specifically:</strong> Plotting Them</p>
<p><img data-orig-file="https://trinkerrstuff.files.wordpress.com/2018/03/fustrated_r.png?w=326&amp;h=229&amp;fit=326%2C229&amp;resize=326%2C229" data-orig-size="873,614" data-medium-file="https://trinkerrstuff.files.wordpress.com/2018/03/fustrated_r.png?w=326&amp;h=229&amp;fit=326%2C229&amp;resize=326%2C229?w=300" data-attachment-id="2237" data-image-meta="{" data-image-description="" src="https://trinkerrstuff.files.wordpress.com/2018/03/fustrated_r.png?w=326&h=229&fit=326%2C229&resize=326%2C229" data-permalink="https://trinkerrstuff.wordpress.com/plot-titles-text-math-notation-expressions__trashed/fustrated_r/" data-large-file="https://trinkerrstuff.files.wordpress.com/2018/03/fustrated_r.png?w=326&amp;h=229&amp;fit=326%2C229&amp;resize=326%2C229?w=640" alt="fustrated_r" data-image-title="fustrated_r" sizes="(max-width: 326px) 100vw, 326px" width="326" data-comments-opened="1" class="wp-image-2237" data-recalc-dims="1" height="229"></p>
<p>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.</p>
<p><strong>Success is if I can easily plot the following title:</strong></p>
<p><img data-orig-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture.png" data-orig-size="1398,198" data-medium-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture.png?w=300" data-attachment-id="2238" data-image-meta="{" data-image-description="" src="https://trinkerrstuff.files.wordpress.com/2018/03/capture4.png?w=234&h=33&fit=234%2C33&resize=234%2C33" data-permalink="https://trinkerrstuff.wordpress.com/plot-titles-text-math-notation-expressions__trashed/capture/" data-large-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture.png?w=640" alt="Capture" data-image-title="Capture" width="234" data-comments-opened="1" class="wp-image-2238" data-recalc-dims="1" height="33"></p>
<p>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.</p>
<h2>Math Notations Require an Expression or Call</h2>
<p>This was my first aha.  I don’t know exactly what an <code>expression</code> or <code>call</code> 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:  <a rel="nofollow" href="http://adv-r.had.co.nz/Expressions.html%C2%A0" target="_blank">http://adv-r.had.co.nz/Expressions.html </a> 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: <a rel="nofollow" href="https://stackoverflow.com/q/20355547/1000343" target="_blank">https://stackoverflow.com/q/20355547/1000343</a>  But let’s get back to the task at hand…a single way to plot the above title with mixed strings, notation, &amp; numbers.</p>
<h2>Use bquote</h2>
<p>I’ve seen mixed string, number, and math expression annotation done with <code>expression</code>, <code>parse(text=</code>, <code>bquote</code>, <code>substitute</code> &amp; clever use of back ticks. For me, the most straight forward way is <code>bquote</code>. It seems to be pretty flexible for most tasks. Here are the four rules to overcome math notation title blues when using <code>bquote</code>:</p>
<ol>
<li><strong>Strings –</strong> Require quotes wrapped w/ tilde separator (e.g., <code>&quot;my text&quot;  ~</code>).</li>
<li><strong>Math Expressions –</strong> Unquoted &amp; follow <a rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/plotmath.html" target="_blank"><code>?plotmath</code></a></li>
<li><strong>Numbers – </strong>Unquoted when part of math notation</li>
<li><strong>Variables –</strong> Use <code>.()</code> (pass in string or numeric)</li>
</ol>
<p>Got that? Great!</p>
<h2>Application</h2>
<p>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:</p>
<p><img data-orig-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture.png" data-orig-size="1398,198" data-medium-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture.png?w=300" data-attachment-id="2238" data-image-meta="{" data-image-description="" src="https://trinkerrstuff.files.wordpress.com/2018/03/capture4.png?w=234&h=33&fit=234%2C33&resize=234%2C33" data-permalink="https://trinkerrstuff.wordpress.com/plot-titles-text-math-notation-expressions__trashed/capture/" data-large-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture.png?w=640" alt="Capture" data-image-title="Capture" width="234" data-comments-opened="1" class="wp-image-2238" data-recalc-dims="1" height="33"></p>
<p>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 <em>cor</em> value is passed in (more on this in a moment)?  If you struggle with the math notation see <a rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/plotmath.html" target="_blank"><code>?plotmath</code></a></p>
<p><img alt="bquote" src="https://trinkerrstuff.files.wordpress.com/2018/03/picture1.png?w=456" class="wp-image-2239" data-recalc-dims="1"></p>
<p>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.</p>
<p><img data-orig-file="https://trinkerrstuff.files.wordpress.com/2018/03/bquote.png" data-orig-size="1595,168" data-medium-file="https://trinkerrstuff.files.wordpress.com/2018/03/bquote.png?w=300" data-attachment-id="2239" data-image-meta="{" data-image-description="" src="https://trinkerrstuff.files.wordpress.com/2018/03/picture2.png?w=456" data-permalink="https://trinkerrstuff.wordpress.com/plot-titles-text-math-notation-expressions__trashed/bquote/" data-large-file="https://trinkerrstuff.files.wordpress.com/2018/03/bquote.png?w=640" alt="bquote" data-image-title="bquote" data-comments-opened="1" class="wp-image-2239" data-recalc-dims="1"></p>
<h2>Example</h2>
<h4>Code:</h4>
<p>I’m going to plot this 2 times.  One where the variable <code>cor</code> being passed in is a double and then a string (<code>cor2</code>).  Notice that the leading zero is used in the double?</p>
<pre>
## A variable to pass in
cor &lt;- -.321
cor2 &lt;- '-.321'

par(mfrow = c(1, 2))
plot(1:10, 1:10, main = bquote(&quot;Hello&quot; ~ r[xy] == .(cor) ~ &quot;and&quot; ~ B^2))
plot(1:10, 1:10, main = bquote(&quot;Hello&quot; ~ r[xy] == .(cor2) ~ &quot;and&quot; ~ B^2))
</pre>
<h4>Results in:</h4>
<p><img data-orig-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture1.png?w=450&amp;h=251&amp;fit=488%2C251" data-orig-size="760,391" data-medium-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture1.png?w=450&amp;h=251&amp;fit=488%2C251?w=300" data-attachment-id="2240" data-image-meta="{" data-image-description="" src="https://trinkerrstuff.files.wordpress.com/2018/03/capture1.png?w=450&h=251&fit=488%2C251" data-permalink="https://trinkerrstuff.wordpress.com/plot-titles-text-math-notation-expressions__trashed/capture-2/" data-large-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture1.png?w=450&amp;h=251&amp;fit=488%2C251?w=640" alt="Capture" data-image-title="Capture" sizes="(max-width: 488px) 100vw, 488px" data-comments-opened="1" class="wp-image-2240" data-recalc-dims="1"></p>
<h4>ggplot2: Me To!</h4>
<p>Works for <strong>ggplot2</strong> as well.</p>
<pre>
library(ggplot2)

ggplot() + labs(title = bquote(&quot;Hello&quot; ~ r[xy] == .(cor2) ~ &quot;and&quot; ~ B^2))
</pre>
<p><img data-orig-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture2.png?w=220&amp;h=168&amp;fit=220%2C168&amp;resize=220%2C168" data-orig-size="366,280" data-medium-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture2.png?w=220&amp;h=168&amp;fit=220%2C168&amp;resize=220%2C168?w=300" data-attachment-id="2241" data-image-meta="{" data-image-description="" src="https://trinkerrstuff.files.wordpress.com/2018/03/capture2.png?w=220&h=168&fit=220%2C168&resize=220%2C168" data-permalink="https://trinkerrstuff.wordpress.com/plot-titles-text-math-notation-expressions__trashed/capture-3/" data-large-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture2.png?w=220&amp;h=168&amp;fit=220%2C168&amp;resize=220%2C168?w=366" alt="Capture" data-image-title="Capture" sizes="(max-width: 220px) 100vw, 220px" width="220" data-comments-opened="1" class="wp-image-2241" data-recalc-dims="1" height="168"></p>
<h2>Another Way</h2>
<p>Alas there is more than one way to accomplish math notation in titles in R.  If you want one way then <code>bquote</code> and the 4 rules will likely always get it done.  Skip this brief section. </p>
<p>If you’re still reading…You can also use a <code>parse(text =</code> and <code>paste</code> method.  The two approaches are similar to a <code>sprintf</code> vs. <code>paste</code> approach to string manipulation (see <a rel="nofollow" href="https://trinkerrstuff.wordpress.com/2013/09/15/paste-paste0-and-sprintf-2/" target="_blank">paste, paste0, and sprintf</a>).  This approach requires a bit more reasoning and the <code>cor2</code> is coerced to a double with a leading zero when it’s evaluate?</p>
<p>There are more ways too but I’ll leave that for the curious reader <img alt="🙂" src="https://i0.wp.com/s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png?w=456&ssl=1" class="wp-smiley" data-recalc-dims="1"><br>
 </p>
<pre>
plot(1:10, 1:10, main = parse(text = paste0('&quot;Hello&quot;', ' ~ r[xy] == ', cor2, '~ B^2')))
</pre>
<p><img data-orig-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture3.png?w=251&amp;h=277&amp;fit=251%2C277&amp;resize=251%2C277" data-orig-size="417,460" data-medium-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture3.png?w=251&amp;h=277&amp;fit=251%2C277&amp;resize=251%2C277?w=272" data-attachment-id="2242" data-image-meta="{" data-image-description="" src="https://trinkerrstuff.files.wordpress.com/2018/03/capture3.png?w=251&h=277&fit=251%2C277&resize=251%2C277" data-permalink="https://trinkerrstuff.wordpress.com/plot-titles-text-math-notation-expressions__trashed/capture-4/" data-large-file="https://trinkerrstuff.files.wordpress.com/2018/03/capture3.png?w=251&amp;h=277&amp;fit=251%2C277&amp;resize=251%2C277?w=417" alt="Capture" data-image-title="Capture" sizes="(max-width: 251px) 100vw, 251px" width="251" data-comments-opened="1" class="wp-image-2242" data-recalc-dims="1" height="277"></p>
<p>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 <code>plotmath</code> and wanted to share: <a rel="nofollow" href="http://vis.supstat.com/2013/04/mathematical-annotation-in-r/" target="_blank">http://vis.supstat.com/2013/04/mathematical-annotation-in-r</a></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://trinkerrstuff.wordpress.com/2018/03/15/2246/"> R – TRinker's R Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/qtSxI0jO-rk" height="1"></a></div>