# How to Use the textxy() Function in R

Often you may want to add text to a specific location in base R plots.

One of the easiest ways to do so is by using the textxy() function from the calibrate package in R, which can be used to perform this exact task.

The textxy() function uses the following syntax:

textxy(X, Y, labs, m, cex, offset)

where:

• X: X coordinates of a set of points
• Y: Y coordinates of a set of points
• labs: Labels to be placed next to the points
• m: coordinates of the origin of the plot (default is (0,0))
• cex: Character expansion factor
• offset: Distance between labels and points (default is 0.8)

The following example shows how to use the textxy() function in practice to add text to a plot.

Note: Before using the textxy() function, you may need to first install the calibrate package. You can use the following syntax to do so:

`install.packages('calibrate')`

Once the calibrate package has been installed, you can proceed to use the textxy() function.

## Example: How to Use the textxy() Function in R

Suppose that we create the following data frame in R that contains information about various basketball players:

```#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
points=c(99, 68, 86, 88, 95, 74, 78, 93),
assists=c(22, 28, 31, 35, 34, 45, 28, 31),
rebounds=c(30, 28, 24, 24, 30, 36, 30, 29))

#view data frame
df

team points assists rebounds
1    A     99      22       30
2    A     68      28       28
3    A     86      31       24
4    A     88      35       24
5    B     95      34       30
6    B     74      45       36
7    B     78      28       30
8    B     93      31       29
```

Now suppose that we would like to create a scatterplot in base R to visualize the relationship between the points and assists variables.

We can use the following syntax to do so:

```#create scatterplot to visualize points vs. assists
plot(df\$assists, df\$points)
```

This produces the following scatterplot:

The x-axis shows the values from the assists column while the y-axis shows the values from the points column of the data frame.

Now suppose that we would like to create this same scatterplot but add labels to each point so that we know which team each point belongs to.

We can use the textxy() function to add these labels and specify team as the column to use for the labs argument of the function:

```library(calibrate)

#create scatterplot with labels
plot(df\$assists, df\$points)
textxy(df\$assists, df\$points, labs=df\$team)
```

This produces the following result:

Notice that the team labels have been added to each point in the plot.

However, the labels overlap with the points and the font size of the labels is rather small.

We can use the cex argument to increase the font size and the offset argument to offset the labels from the points in the plot:

```library(calibrate)

#create scatterplot with labels
plot(df\$assists, df\$points)
textxy(df\$assists, df\$points, labs=df\$team, cex=1, offset=1)```

This produces the following result:

Notice that the labels are much easier to read now and don’t overlap with the points in the plot.

Feel free to play around with the values for the cex and offset arguments to produce a plot with the exact labels that you would like.