Site icon R-bloggers

R plotmath functions combined with variable values

[This article was first published on R-project – lukemiller.org, 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.

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 %+-%, 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):

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 bquote() call. To make the plot above, I used the following commands:

# 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,  = 2, cex = 2)

The bquote() is where the action happens. Inside bquote(), I have the following arguments:

Once mylabel is assembled, I stick it in the plot at a location defined in the text() function.

The complete list of keywords and operator symbols available for use in this way can be found by typing ?plotmath at the R console and perusing the help file.

To leave a comment for the author, please follow the link and comment on their blog: R-project – lukemiller.org.

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.