How to Use geom_label() in ggplot2


You can use the geom_label() function in ggplot2 to add text labels inside a rectangular box to a plot.

This function uses the following basic syntax:

p +
  geom_label()

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

We then use the geom_label( ) 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_label() function in practice in R.

Note: Before using the geom_label() function, you will first need to install the ggplot2 package. You can use the following syntax to do so:

install.packages('ggplot2')              

Once the ggplot2 package is successfully installed, you will be able to use the geom_label() function without encountering any errors.

Example: How to Use geom_label() in ggplot2

Suppose that we create the following data frame 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 the following columns:

  • team: The team name they player belongs on
  • points: The total points scored by the player
  • assists: The total assists made by the player

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 following syntax to do so:

library(ggplot2)

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

This produces the following plot:

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

Suppose that we would like to add labels to each point that displays the team name for each player.

We can use the label argument within the aes() function of ggplot() along with the geom_label() function as follows:

library(ggplot2)

#create scatter plot of points vs assists with labels for team names
ggplot(df, aes(points, assists, label=team)) +
  geom_point() +
  geom_label()

This produces the following plot:

ggplot2 geom_label example

Notice that labels have been added to the plot over each point to indicate the team name that each player belongs on.

Note that you could also specify fontface=’bold’ within the geom_label() function if you would like the text inside of each of the rectangles to be bold:

library(ggplot2)

#create scatter plot of points vs assists with labels for team names
ggplot(df, aes(points, assists, label=team)) +
  geom_point() +
  geom_label(aes(fontface='bold'))

This produces the following result:

Notice that this matches the plot from the previous example except the text labels are now bold, which makes them easier to read.

Note that if you’d like to add text labels without the white rectangular background then you could use the geom_text() function instead.

Note: You can find the complete documentation for the geom_label 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

Leave a Reply

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