Open In App

How to Add Labels Directly in ggplot2 in R

Improve
Improve
Like Article
Like
Save
Share
Report

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().

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. 

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 RGeeksforgeeks

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.
 

Using geom_label()  to Add Labels Directly in ggplot2

This method is used to add Text labels to data points in ggplot2 plots. It pretty much works the same as the geom_text the only difference being it wraps the label inside a rectangle.

Syntax: ggp + geom_label( label,  nudge_x , nudge_y,   check_overlap,  label.padding,  label.size,  color,  fill )

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
  • label.padding: padding inside rectangular overlap
  • label.size: size of rectangular overlap
  • color: color of text in label
  • fill: background color of rectangular overlap

Example: Scatter plot with labels on it using ggplot2 and geom_label().

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_label(
    label=label1,
    nudge_x=0.45, nudge_y=0.1,
    check_overlap=T,
    label.padding=unit(0.55, "lines"),
    label.size=0.4,
    color="white",
    fill="#038225"
)


Output:

 Add Labels Directly in ggplot2 in RGeeksforgeeks

 Add Labels Directly in ggplot2 in R

The x1, y1, and label1 vectors are defined in the following block of code. The labels and related x and y coordinates for the data points are contained in these vectors.

Then, using the aforementioned vectors, a data frame called sample_data is produced.

The plot is started using the ggplot function, and the variables x1 and y1 are mapped to the x and y aesthetics using the aes function.

A point is added to the plot using the geom_point function based on the provided x and y coordinates.

Using the geom_label function, labelled points can be added to the plot. The vector label1, which includes the labels for each point, is specified by the label parameter. The labels are moved a little bit using the nudge_x and nudge_y variables. To avoid label overlap, the check_overlap parameter is set to TRUE.

The other options used in geom_label are listed below:

Using the unit function, the label. padding setting determines the padding around the label text. It is set to 0.55 lines in this instance.
The size of the label text is controlled by a label. size.
colour changes the label text’s colour to white.
fill specifies a specific shade of green (“#038225”) as the fill colour for the label background.

we should be able to generate a scatter plot with labelled points using this code, with the labels appearing as filled text boxes. In order to prevent overlap, the labels are placed adjacent to the respective spots on the plot.



Last Updated : 10 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads