How to Add Superscripts and Subscripts to Plots in R?

[This article was first published on Data Science Tutorials, 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.

The post How to Add Superscripts and Subscripts to Plots in R? appeared first on Data Science Tutorials

How to Add Superscripts and Subscripts to Plots in R?, The basic syntax for adding superscripts or subscripts to charts in R is as follows.

ggplot2 Guide

define superscript expression

x.expression <- expression(x^3 ~ variable ~ label)

define subscript expression

y.expression <- expression(y[3] ~ variable ~ label)

axis labels with expressions

plot(x, y, xlab = x_expression, ylab = y_expression)

How to Add Superscripts and Subscripts to Plots in R?

These examples demonstrate how to apply this syntax in real-world situations.

Example 1: Axis labels with superscripts

The code below demonstrates how to add superscripts to an R plot’s axis labels.

Let’s create some data

x <- c(11, 12, 13, 14, 15, 16, 17, 18)
y <- c(9, 10, 19, 18, 12, 19, 16, 19)

create x and y-axis labels with superscripts

x.expression <- expression(x^3 ~ variable ~ label)
y.expression <- expression(y^3 ~ variable ~ label)

Now we can create a plot

plot(x, y, xlab = x.expression, ylab = y.expression)

Take note of the superscript in the labels of the x- and y-axes.

The graphic has a little gap where the y-axis superscript should be. With the help of R’s par() function, we can bring the axis labels closer to the plot.

adjust par values (default is (3, 0, 0))

par(mgp=c(2.5, 1, 0))
plot(x, y, xlab = x.expression, ylab = y.expression)

Remarkably, we selected “3” at random to go in the superscript. You are permitted to use any character or number as a superscript.

Example 2: Add Axis Labels with Subscripts

The code below demonstrates how to add subscripts to an axis label in an R plot.

x.expression <- expression(x[3] ~ variable ~ label)
y.expression <- expression(y[3] ~ variable ~ label)
plot(x, y, xlab = x.expression, ylab = y.expression)

The post How to Add Superscripts and Subscripts to Plots in R? appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)