13.2 C
London
Tuesday, July 2, 2024
HomeStatistics TutorialRHow to Add Superscripts & Subscripts to Plots in R

How to Add Superscripts & Subscripts to Plots in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

#define expression with superscript
x_expression 3 ~ variable ~ label)

#define expression with subscript
y_expression 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 #define x and y-axis labels with superscripts
x_expression #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 #define x and y-axis labels with superscripts
x_expression #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 #create plot
plot(x, y)

#define label with superscript to add to plot
R2_expression #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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories