How to Use geom_text() in R


You can use the geom_text() function in ggplot2 to add text to a plot.

This function uses the following basic syntax:

p +
  geom_text()

This particular example assumes that a ggplot object has been created and assigned to the variable named p.

We then use the geom_text( ) argument to add text labels that correspond to the variable specified in the label argument of ggplot2.

The following example shows how to use the geom_text() function in practice in R.

Example: How to Use geom_text() in R

For this example we will create the following dataset that contains information about various basketball players:

#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'),
                 points=c(22, 39, 24, 18, 15, 10, 28, 23),
                 assists=c(3, 8, 8, 6, 10, 14, 8, 17))

#view data frame
df

  team points assists
1    A     22       3
2    B     39       8
3    C     24       8
4    D     18       6
5    E     15      10
6    F     10      14
7    G     28       8
8    H     23      17

The dataset contains information about the team name, points scored and total assists for 8 different basketball players.

Suppose that we would like to create a scatter plot to visualize the relationship between the values in the points and assists columns of the data frame.

We can use the geom_point() function from ggplot2 to do so.

If you don’t have the ggplot2 data visualization package already installed then you can use the following syntax to do so:

#install ggplot2 package if necessary
install.packages('ggplot2')

Once the package is installed, you can use the following syntax to create a scatter plot:

library(ggplot2)

#create scatter plot of points vs assists
ggplot(df, aes(points, assists, label=team)) +
  geom_point()

This produces the following plot:

The x-axis represents the points scored by each player and the y-axis represents the total assists for each player.

Notice that we used the labels argument to specify that the team labels should be applied to each point in the plot.

To get these labels to actually appear on the plot, we must add the geom_text() function as follows:

library(ggplot2)

#create scatter plot of points vs assists
ggplot(df, aes(points, assists, label=team)) +
  geom_point() +
  geom_text()

This produces the following plot:

By default, the labels are applied immediately over the points in the plot.

In order to read the labels on the plot easier, we may need to use the nudge_x argument within the geom_text() function to “nudge” the labels to the right:

library(ggplot2)

#create scatter plot of points vs assists
ggplot(df, aes(points, assists, label=team)) +
  geom_point() +
  geom_text(nudge_x=0.05)

This produces the following plot:

Notice that each of the team labels have now been added to each point in the plot and each label has been “nudged” to the right to make it easier to read.

Note that you could also supply a negative value to the nudge_x argument to instead nudge the labels to the left of each point.

Keep in mind that the greater the value you supply to the nudge_x argument, the further the label will be from the point in the plot.

Note: You can find the complete documentation for the scale_x_discrete function in ggplot2 here.

Additional Resources

The following tutorials explain how to perform other common tasks in ggplot2:

How to Use scale_y_continuous in ggplot2
How to Rotate Axis Labels in ggplot2
How to Change Legend Labels in ggplot2

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *