How to use geom_text() to Add Labels Directly in ggplot2 In R Language

This method is used to add Text labels to data points in ggplot2 plots. It positions in the same manner as geom_point() does. 

Syntax: ggp + geom_text( label,  nudge_x , nudge_y,   check_overlap )

Parameters:

  • label: Text labels we want to show at data points
  • nudge_x: shifts the text along X-axis
  • nudge_y: shifts the text along Y-axis
  • check_overlap:  avoids text overlap

Example: Scatter plot with labels on it using ggplot2 and geom_text.

R




# import library ggplot2
library(ggplot2)
 
# Create dataset
x1 <- c(1, 1, 1, 2, 3, 4, 4, 4, 5, 5, 6, 2, 3)
y1 <- c(7, 23, 31, 14, 11, 3, 13, 27, 21, 10, 21, 14, 30)
label1 <- c('Apple', 'Guava', 'Papaya', 'Orange', 'PineApple',
             'Dragon Fruit', 'Kiwi', 'blackberry', 'blueberry',
             'grapes', 'strawberry', 'raspberry', 'Grapefruit')
 
sample_data <- data.frame(x1, y1, label1)
 
# add text with geom_text
ggplot(sample_data, aes(x=x1, y=y1)) +
geom_point() +
geom_text(
    label=label1,
    nudge_x=0.45, nudge_y=0.1,
    check_overlap=T
)


Output:

Add Labels Directly in ggplot2 in R

The ggplot2 library, a popular tool for data visualization in R, is imported in the first line.

The x1, y1, and label1 vectors are created in the following block of code. These vectors each have labels for the data points and numerical values for the x and y positions.

The following line uses the vectors that were previously defined to generate a data frame called sample_data. The vectors are combined column-wise into a data frame using the data.frame method.

Finally, a simple scatter plot is produced using the ggplot tool. The variables x1 and y1 are mapped to the x and y aesthetics, respectively, using the aes function.

On the basis of the provided x and y coordinates, the geom_point function is used to add points to the plot.

The labels for the points are added using the geom_text function. The vector label1, which includes the labels for each point, is specified by the label parameter. To prevent label overlap, the nudge_x and nudge_y variables are used to slightly move the labels’ positions. To make sure the labels do not overlap one another, the check_overlap option is set to TRUE.

a scatter plot with points labeled as different fruits. The labels provide the names of the fruits while the x and y coordinates specify where the spots are located. The labels are presented adjacent to the corresponding points on the plot thanks to the geom_text function.
 

How to Add Labels Directly in ggplot2 in R

Labels are textual entities that have information about the data point they are attached to which helps in determining the context of those data points. In this article, we will discuss how to directly add labels to ggplot2 in R programming language.

To put labels directly in the ggplot2 plot we add data related to the label in the data frame. Then we use functions geom_text() or geom_label() to create label beside every data point. Both the functions work the same with the only difference being in appearance. The geom_label() is a bit more customizable than geom_text().

Similar Reads

Using geom_text() to Add Labels Directly in ggplot2

This method is used to add Text labels to data points in ggplot2 plots. It positions in the same manner as geom_point() does....

Using geom_label()  to Add Labels Directly in ggplot2

...