How to Add Superscripts & Subscripts to Plots in R


You can use the following basic syntax to add superscripts or subscripts to plots in R:

#define expression with superscript
x_expression <- expression(x^3 ~ variable ~ label)

#define expression with subscript
y_expression <- expression(y[3] ~ variable ~ label)

#add expressions to axis labels
plot(x, y, xlab = x_expression, ylab = y_expression)

The following examples show how to use this syntax in practice.

Example 1: Add Superscripts to Axis Labels

The following code shows how to add superscripts to the axis labels of a plot in R:

#define data
x <- c(1, 2, 3, 4, 5, 6, 7, 8)
y <- c(9, 12, 14, 16, 15, 19, 26, 29)

#define x and y-axis labels with superscripts
x_expression <- expression(x^3 ~ variable ~ label)
y_expression <- expression(y^3 ~ variable ~ label)

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

superscript on axis in R plot

Notice that both the x-axis and y-axis have a superscript in their label.

The y-axis superscript is a bit cut off in the plot. To move the axis labels closer to the plot, we can use the par() function in R:

#adjust par values (default is (3, 0, 0))
par(mgp=c(2.5, 1, 0)) 

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

Note: We chose “3” as a random value to place in the superscript. Feel free to place any numeric value or character in the superscript.

Example 2: Add Subscripts to Axis Labels

The following code shows how to add subscripts to the axis labels of a plot in R:

#define data
x <- c(1, 2, 3, 4, 5, 6, 7, 8)
y <- c(9, 12, 14, 16, 15, 19, 26, 29)

#define x and y-axis labels with superscripts
x_expression <- expression(x[3] ~ variable ~ label)
y_expression <- expression(y[3] ~ variable ~ label)

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

subscript in axis labels in R

Example 3: Add Superscripts & Subscripts Inside Plot

The following code shows how to add a superscript to a text element inside a plot:

#define data
x <- c(1, 2, 3, 4, 5, 6, 7, 8)
y <- c(9, 12, 14, 16, 15, 19, 26, 29)

#create plot
plot(x, y)

#define label with superscript to add to plot
R2_expression <- expression(paste(" ", R^2 , "= ", .905))

#add text to plot
text(x = 2, y = 25, label = R2_expression)

Additional Resources

How to Create a Scatterplot in R with Multiple Variables
How to Create Side-by-Side Boxplots in R
How to Overlay Plots in R

Leave a Reply

Your email address will not be published.