Open In App

Adding Text to Plots in R programming – text() and mtext () Function

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Text is defined as a way to describe data related to graphical representation. They work as labels to any pictorial or graphical representation.

1. text() Function in R

text() Function in R Programming Language is used to draw text elements to plots in Base R.

Syntax: text(x, y, labels)

Parameters: 

  • x and y: numeric values specifying the coordinates of the text to plot
  • labels: the text to be written

Returns: Added text to plot 

Example: Add text in plot using text function

We are using the head(mtcars) function to load the first 6 rows of the dataset. The plot() function creates a scatter plot with car weight (wt) on the x-axis and miles per gallon (mpg) on the y-axis. We then use the text() function to label each data point with the corresponding car names from the row names.

R
d <- head(mtcars)

plot(d$wt, d$mpg, 
     main = "Car Weight vs. Mileage",
     xlab = "Weight", ylab = "Mileage",
     pch = 19, col = "darkgreen")

text(d$wt, d$mpg, labels = row.names(d), cex = 0.88, pos = 2, col = "darkgreen")

Output: 

Example 2: Add a mathematical annotation to a plot

We are using the plot() function to create a basic graph with points ranging from 1 to 5. The text() function is then used to add mathematical expressions to the plot. The first text() call adds the formula for beta hat (a linear regression coefficient), and the second text() call adds the formula for the mean (bar(x)). The expressions are formatted using R’s expression() function to display mathematical notation.

R
plot(1:5, 1:5, main = "text() Function examples")

text(2, 3, expression(hat(beta) == (X^t * X)^(-1) %*% X^t * y))
text(3, 4, expression(bar(x) == sum(frac(x[i], n), i == 1, n)))

Output:

2. mtext() Function in R

mtext() function in R Programming Language is used to add text to the margins of the plot. 

Syntax: mtext(text, side)

Parameters: 

  • text: text to be written
  • side: An integer specifying the side of the plot, such as: bottom, left, top, and right.

Returns: Added text in the margins of the graph 

Example : Adding Text to Multiple Margins using mtext()

We create a scatter plot using the plot() function with the cars dataset. The mtext() function adds text annotations to the plot’s margins: “Left Margin” in blue on the left, “Right Margin” in green on the right, “Top Margin” in red at the top, and “Bottom Margin” in purple at the bottom. The side parameter defines the margin, line adjusts the position, col sets the text color, and cex controls the text size.

R
plot(cars$speed, cars$dist, main = "Scatter Plot", xlab = "Speed", ylab = "Distance")

mtext("Left Margin", side = 2, line = 2, col = "blue", cex = 1.2)
mtext("Right Margin", side = 4, line = 0, col = "green", cex = 1.2)
mtext("Top Margin", side = 3, line = 0, col = "red", cex = 1.2)
mtext("Bottom Margin", side = 1, line = 2, col = "purple", cex = 1.2)

Output:

gh

Adding Text to Plots in R programming – text() and mtext () Functi

In this article we explored the text() and mtext() functions , that can be used in our plots.



Next Article
Article Tags :

Similar Reads