How to display a variable with subscript ggplot2 graph in R?
Last Updated :
23 Jul, 2025
In R, the ggplot2
package is one of the most popular and powerful tools for creating visually appealing and customizable plots. Sometimes, you may need to display mathematical notation or subscripts in your graphs, especially when working with scientific data or equations. This article will guide you through the process of displaying a variable with a subscript on a ggplot2
graph, providing full theory and examples.
How Does Subscript Work in R?
In R Language subscripts (and other mathematical notations) can be added to text or plot labels using plotmath expressions. Plotmath is an R syntax that lets you display mathematical expressions in your plots. To add subscripts, you can use expression()
and the square bracket [ ]
notation.
expression(x[1])
Now we will discuss Steps for Displaying Subscript in ggplot2
Graphs using R Programming Language.
Step 1: Install and Load ggplot2
Before we start, make sure that you have the ggplot2
package installed and loaded in your R environment.
R
# Install ggplot2 if not installed
install.packages("ggplot2")
# Load the package
library(ggplot2)
Step 2: Prepare the Dataset
We’ll create a simple dataset to demonstrate how to plot a variable with a subscript.
R
# Create a simple dataset
data <- data.frame(
x = 1:10,
y = c(2, 4, 5, 7, 6, 8, 9, 11, 12, 14)
)
Step 3: Basic ggplot2 Graph
Here’s how you would create a basic scatterplot using ggplot2
.
R
# Basic scatter plot
ggplot(data, aes(x = x, y = y)) +
geom_point() +
labs(x = "X Axis", y = "Y Axis") +
ggtitle("Basic Scatter Plot")
Output:
Basic ggplot2 GraphThis plot will not have any subscripts yet. We’ll add subscripts in the next steps.
Step 4: Adding Subscript to Axis Labels
Let’s say we want to represent the x-axis label as X1X_1X1. To achieve this, we use the expression()
function combined with ggplot2
’s labs()
function.
R
# Scatter plot with subscript in x-axis label
ggplot(data, aes(x = x, y = y)) +
geom_point() +
labs(x = expression(X[1]), y = "Y Axis") +
ggtitle("Scatter Plot with Subscript on X Axis")
Output:
Adding Subscript to Axis LabelsIn this example, the expression(X[1])
ensures that the x-axis label is displayed with a subscript.
Step 5: Adding Subscript to Title or Legend
Subscripts can also be added to titles, legends, or other text annotations in your graph. For example, if you want to add a subscript to the plot title, you would use expression()
within the ggtitle()
function.
R
# Scatter plot with subscript in title
ggplot(data, aes(x = x, y = y)) +
geom_point() +
labs(x = "X Axis", y = "Y Axis") +
ggtitle(expression("Scatter Plot of X"[1]~"vs Y"))
Output:
Adding Subscript to Title or LegendIn this case, the title will display as "Scatter Plot of X1X_1X1 vs Y".
Step 6: Using Subscript in Legends
If you have a plot with multiple categories and a legend, you can use subscripts in the legend labels by combining expression()
with the scale_color_manual()
or scale_fill_manual()
functions.
R
# Add a category to the dataset
data$group <- rep(c("A", "B"), each = 5)
# Scatter plot with subscript in legend labels
ggplot(data, aes(x = x, y = y, color = group)) +
geom_point() +
scale_color_manual(
values = c("A" = "blue", "B" = "red"),
labels = c(expression("Group A"[1]), expression("Group B"[2]))
) +
labs(x = "X Axis", y = "Y Axis", color = "Groups") +
ggtitle("Scatter Plot with Subscript in Legend")
Output:
Using Subscript in LegendsIn this example, the legend will display "Group A1A_1A1" and "Group B2B_2B2" with subscripts for each group.
Additional Customizations
If you want to add annotations to the plot with subscripts, you can use annotate()
along with expression()
:
R
# Scatter plot with annotation containing subscript
ggplot(data, aes(x = x, y = y)) +
geom_point() +
annotate("text", x = 5, y = 10, label = expression("Point at X"[1] == 5)) +
labs(x = expression(X[1]), y = "Y Axis") +
ggtitle("Scatter Plot with Annotation")
Output:
Subscript in AnnotationsThis will add a text annotation at the specified coordinates with the subscripted variable.
Conclusion
Using subscripts in ggplot2
plots is essential for scientific and academic presentations, especially when dealing with mathematical notations or equations. With the help of the expression()
function and the powerful customization features of ggplot2
, you can easily incorporate subscripts in your titles, axis labels, legends, and annotations. These techniques enhance the clarity and readability of your visualizations.