How to Add Superscripts and Subscripts to Plots in R?

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)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

5 × three =