In this article, you’ll learn how to add text to a plot. We will also be creating a plot and will try various aesthetic manipulations in the same plot, like changing the text color.

## Add texts within the plot

The `text()` function can be used to draw text inside the plotting area. A simplified format of the function is :

### Usage

``````text(x, y, labels, adj = NULL, pos = NULL, offset = 0.5, vfont = NULL,
cex = 1, col = NULL, font = NULL ...)``````

### Arguments

Firstly, we need a plot so that we can add text to that plot. For this example, we are gonna `mtcars` dataset plot.

``````# Creating a plot
> plot(mtcars\$wt, mtcars\$mpg,
main = " Car Weight vs. Mileage ",
xlab = "Miles", ylab = " Weight",
pch = 17, col = "red")``````

Let’s add text to the plot, adding car name to each marker in the plot.

``````# Adding text to the plot
> text(mtcars\$wt, mtcars\$mpg,  row.names(mtcars),
cex = 0.88, pos = 2, col = "grey") ``````

## Add mathematical annotation to a plot

We can also add a mathematical annotation in the `text()` function, Simply by writing the expression in the `expression()` function.

### Usage

``````expression(…)

is.expression(x)
as.expression(x, …)``````

### Example:

``````# Creating a expression
> equation <- expression(y == x^2 + 2*x + 8)

# Creating a plot
> plot(x=10, y=10, main=equation)``````

In above example the the `text()` function is used to add mathematical annotation to a plot.

## Add text to the margins of the plot

To write text into the margins of a Plot we need to use to `mtext()` function. Text is written in one of the four margins of the current figure region or one of the outer margins of the device region.

### Usage

``````> mtext(text, side = 3, line = 0, outer = FALSE, at = NULL,

### Arguments

In the same way, let’s create a plot and give it some margin text.

``````# Creating a plot
> plot(x=0:10, y=20:30, main="mtext example", col.main="blue")

Here, in the above example, the side specifies the side of the plot such as the bottom, left, top, right. And in the given the `side=3` i.e the top part of the plot and `adj=1` means right or top alignment.

## Conclusion

Congratulations, you learn how to add text in a plot, with how to adjust those text along with margin text with `mtext()` function and how to write expressions using `expression()` function.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.