# How to Label Points on a Scatter plot in R

Scatter plots are a fundamental tool in data visualization and analysis. They provide a clear picture of the relationship between two numerical variables, offering insights into patterns, trends, correlations, and outliers. However, a scatter plot’s real power emerges when we add a third dimension – annotations or labels to each point – which can significantly enhance our understanding of the data.

In R, several functions exist that can help you add labels to your scatter plot, including the ‘text’, ‘geom_text’, or ‘geom_label’ functions. In this comprehensive guide, we will explore these and other techniques in detail. We will start by explaining how to create basic scatter plots before diving into how to add point labels and customize them.

## 1. Preliminary Steps: Data Preparation and Plot Creation

Before labeling points, you must prepare your data and create a scatter plot.

### Data Preparation

First, load your dataset. You can use built-in datasets in R, such as ‘mtcars’ or ‘iris’, or import your data with read.csv(), read_excel(), etc. Let’s use the ‘mtcars’ dataset:

data(mtcars)

We’ll use the ‘mtcars’ dataset to create a scatter plot of ‘mpg’ (miles per gallon) against ‘wt’ (weight).

### Scatter Plot Creation

For scatter plots in R, you can use the plot() function from the base R or ggplot() function from the ‘ggplot2’ library.

#### Base R Method:

plot(mtcars$wt, mtcars$mpg, main="Scatterplot of mpg against wt",
xlab="Car Weight", ylab="Miles Per Gallon", pch=19)

#### ggplot2 Method:

library(ggplot2)

ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
labs(title="Scatterplot of mpg against wt",
x="Car Weight", y="Miles Per Gallon")

In both cases, a scatter plot of ‘mpg’ against ‘wt’ will be displayed.

## 2. Adding Labels to Points in Scatter Plot

Now, let’s label points on the scatter plot.

### Base R Method:

Use the text() function. You need to specify the x and y coordinates, and the labels you want to display. Let’s label the points with the car names:

plot(mtcars$wt, mtcars$mpg, main="Scatterplot of mpg against wt",
xlab="Car Weight", ylab="Miles Per Gallon", pch=19)

text(mtcars$wt, mtcars$mpg, labels=row.names(mtcars), cex=0.7, pos=4)

The ‘cex’ argument controls the text size, and ‘pos’ controls the position of the text regarding the point. ‘pos=4’ puts the labels to the right of the points.

### ggplot2 Method:

Use the geom_text() or geom_label() function to add labels. Here’s an example with geom_text():

ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_text(aes(label=row.names(mtcars)), vjust=-1, size=3) +
labs(title="Scatterplot of mpg against wt",
x="Car Weight", y="Miles Per Gallon")

In this case, ‘vjust’ is used to adjust the vertical position of the labels (negative values move the text up), and ‘size’ controls the text size.

## 3. Customizing Point Labels

Now that you know how to label points, let’s customize them further.

### Base R Method:

The text() function provides several arguments for customization, such as ‘col’ for color, ‘font’ for the type of font, etc. For example:

plot(mtcars$wt, mtcars$mpg, main="Scatterplot of mpg against wt",
xlab="Car Weight", ylab="Miles Per Gallon", pch=19)

text(mtcars$wt, mtcars$mpg, labels=row.names(mtcars), cex=0.7, pos=4, col="red", font=2)

This will display the labels in red and in bold (‘font=2’).

### ggplot2 Method:

Customization in ggplot2 is similar. Here is an example with custom colors and font sizes:

ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_text(aes(label=row.names(mtcars)), vjust=-1, size=3, color="blue", family="Times") +
labs(title="Scatterplot of mpg against wt",
x="Car Weight", y="Miles Per Gallon")

## 4. Handling Overlapping Labels

A common problem in scatter plots is overlapping labels, which makes them hard to read.

### Base R Method:

There’s no direct way in base R to handle overlapping labels, but you can manually adjust the position of labels using the ‘adj’ argument in the text() function.

### ggplot2 Method:

The ‘ggrepel’ package in ggplot2 helps to avoid overlapping of labels.

install.packages("ggrepel")
library(ggrepel)

ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_text_repel(aes(label=row.names(mtcars)), size=3) +
labs(title="Scatterplot of mpg against wt",
x="Car Weight", y="Miles Per Gallon")

In conclusion, labeling points on a scatter plot is a great way to provide more context or highlight specific data points. While this guide provides many options for adding labels to a scatter plot in R, the best method will depend on your specific needs and the complexity of your data. Regardless of the method you choose, adding labels can improve the interpretability and meaningfulness of your scatter plot.

Posted in RTagged