# How to Add Text to a Plot in R

In base R, the text() function can be used to add text to a plot.

The following is the fundamental syntax for this function

`text(x, y, “my text”)`

where:

x, y: The text should be positioned at (x, y) coordinates.

eXtreme Gradient Boosting in R » Ultimate Guide » finnstats

## How to Add Text to a Plot in R

The examples below demonstrate how to utilize this function in practice.

### Example 1: Add One Text Element to Plot

The code below demonstrates how to use text() to add one text element to a plot at the (x, y) coordinates.

Let’s create a data frame with values to a plot

```df <- data.frame(x=c(1, 3, 5, 6, 9, 10),
y=c(2, 8, 10, 11, 12, 16))
df```
```    x  y
1  1  2
2  3  8
3  5 10
4  6 11
5  9 12
6 10 16```

Now we can create a scatterplot

`plot(df\$x, df\$y)`

Now add text element at (5, 8)

Coefficient of Variation Example » Acceptable | Not Acceptable» finnstats

`text(x=5, y=18, "finnstats.com")` Notice how our text element has been inserted into the plot’s (5, 8) (x, y) coordinates.

### Example 2: Plot with Multiple Text Elements

We can easily utilize multiple text() routines to add several text pieces to a plot. Create a data frame with values to plot

```df <- data.frame(x=c(1, 3, 5, 6, 9, 10),
y=c(2, 8, 10, 11, 12, 16))```

Let’s create a scatterplot

`plot(df\$x, df\$y)`

```text(x=2, y=8, "finnstats.com")
text(x=5, y=10, "finnstats")
text(x=8, y=12, "data analysis")``` Three text components, each at the (x, y) locations we specified, have been placed in the plot.

Equality of Variances in R-Homogeneity test-Quick Guide » finnstats

### Example 3: Plot Text Elements Can Be Customized

The cex, col, and font options can be used to change the size, colour, and font style of the plot’s text elements, respectively:

make a data frame with the values you want to plot

```df <- data.frame(x=c(1, 3, 5, 6, 9, 10),
y=c(2, 8, 10, 11, 12, 16))
plot(df\$x, df\$y)```

add text elements with custom appearance

```text(x=5, y=18, "first text", col='red')
text(x=5, y=10, "second text", col='blue', cex=3)
text(x=5, y=5, "third text", col='green', cex=5, font=3)``` ### Example 4: Add Text Labels to Each Point in Plot

We can use the labels arguments to add a text label to each point in the plot.

```df <- data.frame(Country=c('A', 'B', 'C', 'D', 'E', 'F'),
x=c(1, 3, 5, 6, 9, 10),
y=c(2, 8, 10, 11, 12, 16))
df```
```   Country  x  y
1       A  1  2
2       B  3  8
3       C  5 10
4       D  6 11
5       E  9 12
6       F 10 16```

create a scatterplot

`plot(df\$x, df\$y)`

add a text label to each point in the plot

Descriptive Statistics in R » finnstats

`text(df\$x, df\$y, labels=df\$teams, pos=4)` You’ll see that each of the plot’s points now has a text label.

Also, the pos argument, which has four possible values, determines where the text label is positioned in relation to the point:

1: To the text below

2: To the left of the text

3: To the above of the text

4: text to the right

Each text label is put to the right of the points in the plot because we specified pos=4.