<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/r-plotmath-functions-combined-with-variable-values/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://lukemiller.org/index.php/2017/05/r-plotmath-functions-combined-with-variable-values/"> R-project – lukemiller.org</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Getting certain special symbols into R plots, combined with values that are currently stored in variables, has been an ongoing headache of mine. In particular, plotmath symbols such as the plus-minus sign (±), for which the plotmath command is <code>%+-%</code>, had always caused problems due to my inability to parse the R help documentation. For an example, I was trying to get the following simple value (a mean ± 1 SE for a temperature difference) inserted into a plot (a minimalist example):<br>
<a target="_blank" rel="nofollow" href="https://i2.wp.com/lukemiller.org/wp-content/uploads/2017/05/plotlabel-1.png"><img sizes="(max-width: 500px) 100vw, 500px" alt="" class="wp-image-2382" src="https://i2.wp.com/lukemiller.org/wp-content/uploads/2017/05/plotlabel-1.png?w=450" data-recalc-dims="1"></a></p>
<p>It turns out that the plotmath operators that are surrounded by percent signs need to be surrounded by empty quote marks to get them to appear correctly inside a <code>bquote()</code> call. To make the plot above, I used the following commands:</p>
<pre>
# Define some variables
mymean = 1.2345678
mySE = 0.55555
# Make the empty plot
plot(x = 0, y = 0, type = 'n')
# Assemble a label, using bquote()
mylabel = bquote(Delta*italic(T)[max]~.(format(mymean,digits=3))*''%+-%''*
				.(format(mySE,digits=2))*degree*C)
# Add mylabel to the plot at x=0, y=0
text(x = 0, y = 0, labels = mylabel, font = 2, cex = 2)
</pre>
<p>The <code>bquote()</code> is where the action happens. Inside <code>bquote()</code>, I have the following arguments:</p>
<ul>
<li> <code>Delta</code> gets interpreted as a keyword, and turned in to the greek symbol.
<li> The asterisk makes sure Delta is directly next to whatever follows the asterisk (instead of the ~ tilde symbol, which inserts a blank space).
<li> <code>italic(T)</code> makes the capital letter T appear in italics.
<li> <code>[max]</code> makes whatever is inside the brackets into a subscript. A superscript would be set using the ^ symbol.
<li> The tilde puts a space after the subscript
<li> <code>.(format(mymean,dig=3))</code> evaluates the variable inside <code>.()</code> and returns the value. In this case, I have inserted a call to <code>format()</code> to make sure the value of <code>mymean</code> is displayed with a sensible number of significant digits.
<li> The asterisk leads into the <code>''%+-%''</code> chunk. The empty quotes around <code>%+-%</code> allow it to work here and be converted into the ± symbol.
<li> The next asterisk leads into <code>.(format(mySE,digits=2))</code>, which is another call to <code>.()</code> used to retrieve and print the numeric value stored in <code>mySE</code>, with the appropriate number of digits.
<li> The next asterisk leads into <code>degree</code>, which is another keyword, and R will convert it into the degree symbol °.
<li> Finally, the next asterisk leads to <code>C</code>, which just gets interpreted as the letter C, since it is not a keyword. I could have written a whole word here if desired (Celsius), or even multiple words separated by ~ tildes.
</ul>
<p>Once <code>mylabel</code> is assembled, I stick it in the plot at a location defined in the <code>text()</code> function. </p>
<p>The complete list of keywords and operator symbols available for use in this way can be found by typing <code>?plotmath</code> at the R console and perusing the help file. </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="http://lukemiller.org/index.php/2017/05/r-plotmath-functions-combined-with-variable-values/"> R-project – lukemiller.org</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/SRW-sGd8EhM"></a></div>