Producing graphs for scientific journals is rarely ever done without adding some special scientific and mathematical notations, such as subscripts, superscripts, symbols, and other notations. In this recipe, we will learn how to add these to annotations to our graphs.
We will only use base graphics functions for this recipe. So, just open up the R prompt and type in the following code. We will use the airpollution.csv
example dataset for this recipe. So, let's first load it:
air<-read.csv("airpollution.csv")
Let's make a scatter plot of concentrations of particulate matter versus nitrogen oxides and add titles with subscripts as in PM10 and NOX and units mg m-3:
plot(air,las=1, main=expression(paste("Relationship between ",PM[10]," and ",NO[X])), xlab=expression(paste(NO[X]," concentrations (",mu*g^-3,")")), ylab=expression(paste(PM[10]," concentrations (",mu*g^-3,")")))
In the example, we added three new elements of special formatting and notation: subscripts, superscripts, and a Greek symbol (m), using the expression()
function.
The expression()
function accepts arguments in a predefined syntax and translates them into the desired format or symbol. For example, any characters enclosed within square brackets []
are converted to subscripts, such as the X in NOX and 10 in PM10. Similary, any characters following the ^
sign are converted to superscripts, such as the -3 power value in mg m-3. The letters, mu
, are converted to symbol m, denoting micro.
In the example, we used a combination of regular text and expressions by using the expression()
function with the paste()
function.
There are a lot more options and functions we can use inside expression()
to create much more advanced notations than subscripts and superscripts. For example, integral()
, frac()
, sqrt()
, and sum()
can be used to create mathematical signs for integrals, fractions, square roots, and sums, respectively.
To see and learn all the possible options and symbols, run the following command at the R prompt:
demo(plotmath)
You will see the following symbols displayed on the plot device. You will need to press the Return or Enter key to progress through each set of symbols:
3.148.107.193