Table of Contents
Superscripts and subscripts are often used in scientific and mathematical notation to represent exponents and indices. In R, these characters can also be added to plots in order to enhance the visual representation of data. To add superscripts and subscripts to plots in R, the expression() function can be used to create and format text with these characters. This function takes in two arguments, the text to be formatted and the format of the text, which can include the use of superscripts and subscripts. By incorporating the expression() function into the plot labels and titles, users can easily add these special characters to their plots in R. This feature allows for clearer and more accurate representation of data, making it a useful tool for scientists, mathematicians, and data analysts.
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)

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 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)

Example 3: Add Superscripts & Subscripts Inside 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)

Cite this article
stats writer (2024). How can I add superscripts and subscripts to plots in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-superscripts-and-subscripts-to-plots-in-r/
stats writer. "How can I add superscripts and subscripts to plots in R?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-superscripts-and-subscripts-to-plots-in-r/.
stats writer. "How can I add superscripts and subscripts to plots in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-superscripts-and-subscripts-to-plots-in-r/.
stats writer (2024) 'How can I add superscripts and subscripts to plots in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-superscripts-and-subscripts-to-plots-in-r/.
[1] stats writer, "How can I add superscripts and subscripts to plots in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I add superscripts and subscripts to plots in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
