R-exercises – Mathematical Expressions in R Plots: Tutorial (2024)

You are here: Home / Tutorials / Mathematical Expressions in R Plots: Tutorial

by Easton White 1 Comment

It is quite common to want to use mathematical expressions in R. Specifically, mathematical symbols or entire equations may be needed when building plots.
In this tutorial, we will examine how mathematical expressions can be included into R graphics.
We will use the co2 data already found in R. The data includes the atmospheric concentrations of CO2 in parts per million. The data was recorded monthly at the Mauna Loa Observatory in Hawaii between 1959 and 1997.

The following code loads the data and then creates a simple plot of CO2 versus time.

data("co2")plot(co2)

R-exercises – Mathematical Expressions in R Plots: Tutorial (2)

This is a bit ugly and there is room to clean up the plot.

We can use expression() to include mathematical expressions in the plot labels.

plot(co2,ylab = expression("Atmospheric concentration of CO"[2]),las=1)

R-exercises – Mathematical Expressions in R Plots: Tutorial (3)

We now see that CO2 on the Y-label has a subscript for the number 2. We can include more advanced mathematical expressions as well. For example, let’s add a title to the plot.

plot(co2,ylab = expression("Atmospheric concentration of CO"[2]), main = expression(paste("CO"[2]," measured in ",frac('parts','million'),sep='')),las=1)

R-exercises – Mathematical Expressions in R Plots: Tutorial (4)

R-exercises – Mathematical Expressions in R Plots: Tutorial (5)

Learn more about the application of mathematical expressions in the online course R Programming for Simulation and Monte Carlo Methods. In this course you will learn how to:

  • Work extensively with mathematical simulations,
  • Learn the background math of these simulations and how mathematical expressions can simplify your work,
  • And much more

Notice that here we used expression() in combination with paste() to combine multiple pieces of information.

If you want to include a variable within a mathematical expression, you need to use substitute() instead of expression().

plot(co2,ylab = expression("Atmospheric concentration of CO"[2]), main = expression(paste("CO"[2]," measured in ",frac('parts','million'),sep='')),las=1)text(1994,y = 340,labels = substitute(paste("CO"[2],'=',CO2_level,sep=''),list(CO2_level=364)))arrows(1994,342,1998,364.34,length = 0.2,col='red')

R-exercises – Mathematical Expressions in R Plots: Tutorial (6)

The plot shows how text() can be used with substitute() to insert mathematical notation with a variable CO2_level that can be altered or updated.

As you can imagine, there are lots of possible mathematical expressions, depending on the specific context. You can use ?plotmath or demo(plotmath) to explore more options.

Here are some possibilities which can be found by using ?plotmath.

SyntaxMeaning
x + yx plus y
x - yx minus y
x*yjuxtapose x and y
x/yx forwardslash y
x %+-% yx plus or minus y
x %/% yx divided by y
x %*% yx times y
x %.% yx cdot y
x[i]x subscript i
x^2x superscript 2
paste(x, y, z)juxtapose x, y, and z
sqrt(x)square root of x
sqrt(x, y)yth root of x
x == yx equals y
x != yx is not equal to y
x < yx is less than y
x <= yx is less than or equal to y
x > yx is greater than y
x >= yx is greater than or equal to y
x %~~% yx is approximately equal to y
x %=~% yx and y are congruent
x %==% yx is defined as y
x %prop% yx is proportional to y
plain(x)draw x in normal font
bold(x)draw x in bold font
italic(x)draw x in italic font
bolditalic(x)draw x in bolditalic font
list(x, y, z)comma-separated list
...ellipsis (height varies)
cdotsellipsis (vertically centred)
ldotsellipsis (at baseline)
x %subset% yx is a proper subset of y
x %subseteq% yx is a subset of y
x %notsubset% yx is not a subset of y
x %supset% yx is a proper superset of y
x %supseteq% yx is a superset of y
x %in% yx is an element of y
x %notin% yx is not an element of y
hat(x)x with a circumflex
tilde(x)x with a tilde
dot(x)x with a dot
ring(x)x with a ring
bar(xy)xy with bar
widehat(xy)xy with a wide circumflex
widetilde(xy)xy with a wide tilde
x %<->% yx double-arrow y
x %->% yx right-arrow y
x %<-% yx left-arrow y
x %up% yx up-arrow y
x %down% yx down-arrow y
x %<=>% yx is equivalent to y
x %=>% yx implies y
x %<=% yy implies x
x %dblup% yx double-up-arrow y
x %dbldown% yx double-down-arrow y
alphaomegaGreek symbols
AlphaOmegauppercase Greek symbols
theta1, phi1, sigma1, omega1cursive Greek symbols
Upsilon1capital upsilon with hook
infinityinfinity symbol
partialdiffpartial differential symbol
32*degree32 degrees
60*minute60 minutes of angle
30*second30 seconds of angle
displaystyle(x)draw x in normal size (extra spacing)
textstyle(x)draw x in normal size
scriptstyle(x)draw x in small size
scriptscriptstyle(x)draw x in very small size
underline(x)draw x underlined
x ~~ yput extra space between x and y
x + phantom(0) + yleave gap for “0”, but don’t draw it
x + over(1, phantom(0))leave vertical gap for “0” (don’t draw)
frac(x, y)x over y
over(x, y)x over y
atop(x, y)x over y (no horizontal bar)
sum(x[i], i==1, n)sum x[i] for i equals 1 to n
prod(plain(P)(X==x), x)product of P(X=x) for all values of x
integral(f(x)*dx, a, b)definite integral of f(x) wrt x
union(A[i], i==1, n)union of A[i] for i equals 1 to n
intersect(A[i], i==1, n)intersection of A[i]
lim(f(x), x %->% 0)limit of f(x) as x tends to 0
min(g(x), x > 0)minimum of g(x) for x greater than 0
inf(S)infimum of S
sup(S)supremum of S
x^y + znormal operator precedence
x^(y + z)visible grouping of operands
x^{y + z}invisible grouping of operands
group("(",list(a, b),"]")specify left and right delimiters
bgroup("(",atop(x,y),")")use scalable delimiters
group(lceil, x, rceil)special delimiters

Related exercise sets:

Spatial Data Analysis: Introduction to Raster Processing (Part 1)Spatial Data Analysis: Introduction to Raster Processing: Part-3Advanced Techniques With Raster Data: Part 1 – Unsupervised ClassificationAdvanced Techniques With Raster Data – Part 3: ExercisesSpatial Data Analysis: Introduction to Raster Processing (Part 2)

Reader Interactions

Comments

  1. R-exercises – Mathematical Expressions in R Plots: Tutorial (12)LE says

    Your plots here are no longer rendering on either safari or chrome.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

R-exercises –   Mathematical Expressions in R Plots: Tutorial (2024)
Top Articles
Latest Posts
Article information

Author: Velia Krajcik

Last Updated:

Views: 5431

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.