{"id":2371,"date":"2017-05-04T16:54:10","date_gmt":"2017-05-04T23:54:10","guid":{"rendered":"http:\/\/lukemiller.org\/?p=2371"},"modified":"2017-05-04T16:54:10","modified_gmt":"2017-05-04T23:54:10","slug":"r-plotmath-functions-combined-with-variable-values","status":"publish","type":"post","link":"https:\/\/lukemiller.org\/index.php\/2017\/05\/r-plotmath-functions-combined-with-variable-values\/","title":{"rendered":"R plotmath functions combined with variable values"},"content":{"rendered":"<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 (\u00b1), 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 \u00b1 1 SE for a temperature difference) inserted into a plot (a minimalist example):<br \/>\n<a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2017\/05\/plotlabel-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2017\/05\/plotlabel-1.png\" alt=\"\" width=\"500\" height=\"200\" class=\"aligncenter size-full wp-image-2382\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2017\/05\/plotlabel-1.png 500w, https:\/\/lukemiller.org\/wp-content\/uploads\/2017\/05\/plotlabel-1-300x120.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<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>\n<p>[code lang=&#8221;R&#8221;]<br \/>\n# Define some variables<br \/>\nmymean = 1.2345678<br \/>\nmySE = 0.55555<br \/>\n# Make the empty plot<br \/>\nplot(x = 0, y = 0, type = &#8216;n&#8217;)<br \/>\n# Assemble a label, using bquote()<br \/>\nmylabel = bquote(Delta*italic(T)[max]~.(format(mymean,digits=3))*&#8221;%+-%&#8221;*<br \/>\n\t\t\t\t.(format(mySE,digits=2))*degree*C)<br \/>\n# Add mylabel to the plot at x=0, y=0<br \/>\ntext(x = 0, y = 0, labels = mylabel, font = 2, cex = 2)<br \/>\n[\/code]<\/p>\n<p>The <code>bquote()<\/code> is where the action happens. Inside <code>bquote()<\/code>, I have the following arguments:<\/p>\n<ul>\n<li> <code>Delta<\/code> gets interpreted as a keyword, and turned in to the greek symbol.\n<li> The asterisk makes sure Delta is directly next to whatever follows the asterisk (instead of the ~ tilde symbol, which inserts a blank space).\n<li> <code>italic(T)<\/code> makes the capital letter T appear in italics.\n<li> <code>[max]<\/code> makes whatever is inside the brackets into a subscript. A superscript would be set using the ^ symbol.\n<li> The tilde puts a space after the subscript\n<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.\n<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 \u00b1 symbol.\n<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.\n<li> The next asterisk leads into <code>degree<\/code>, which is another keyword, and R will convert it into the degree symbol \u00b0.\n<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.\n<\/ul>\n<p>Once <code>mylabel<\/code> is assembled, I stick it in the plot at a location defined in the <code>text()<\/code> function. <\/p>\n<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>\n","protected":false},"excerpt":{"rendered":"<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 (\u00b1), for which the plotmath command is %+-%, had always caused problems due to my inability to parse the R help documentation. For [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[218],"tags":[],"class_list":["post-2371","post","type-post","status-publish","format-standard","hentry","category-r-project"],"_links":{"self":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/2371","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/comments?post=2371"}],"version-history":[{"count":14,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/2371\/revisions"}],"predecessor-version":[{"id":2387,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/2371\/revisions\/2387"}],"wp:attachment":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/media?parent=2371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/categories?post=2371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/tags?post=2371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}