Adding Text to Plots in R programming - text() and mtext () Function
Last Updated :
30 Apr, 2025
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:
Adding Text to Plots in R programming - text() and mtext () FunctiIn this article we explored the text() and mtext() functions , that can be used in our plots.
Similar Reads
Adding axis to a Plot in R programming - axis () Function axis() function in R Language is to add axis to a plot. It takes side of the plot where axis is to be drawn as argument. Syntax: axis(side, at=NULL, labels=TRUE) Parameters: side: It defines the side of the plot the axis is to be drawn on possible values such as below, left, above, and right. at: Po
2 min read
Addition of Lines to a Plot in R Programming - lines() Function In R, the lines() function is called to add on top of already existing plot. This is particularly helpful when you want to add more lines, such as trend lines, regression lines, or special lines, on a plot. The lines() function allows flexibility in line color, line width, and line type, with multip
3 min read
Plotting of Data using Generic plots in R Programming - plot() Function In this article, we will discuss how we plot data using Generic plots in R Programming Language using plot() Function. plot functionplot() function in R Programming Language is defined as a generic function for plotting. It can be used to create basic graphs of a different type. Syntax: plot(x, y, t
5 min read
Adding x and y Axis Label to ggplot-grid Built with cowplot in R ggplot2 is one of the most widely used libraries for creating elegant data visualizations in R. Often, you will want to arrange multiple plots together to create a grid layout for comparison or presentation purposes. cowplot is a popular extension that simplifies the arrangement of multiple ggplot o
4 min read
How To Import Text File As A String In R IntroductionUsing text files is a common task in data analysis and manipulation. R Programming Language is a robust statistical programming language that offers several functions for effectively managing text files. Importing a text file's contents as a string is one such task. The purpose of this a
6 min read