youtube video link with explanations for these examples https://youtu.be/-hC0ZaFLivQ

Setup

Call the packages to use and prepare the data for use in this presentation.

# library
library(ggplot2)
library(ggrepel)
library(tibble)


# Create a smaller dataset with 30 rows 
data=head(mtcars, 30)
data <- data%>%
  tibble::rownames_to_column(var ="CarModel")

Using the geom_text to create a label

You can use the other parameters as shown to control the look and feel of the labels

# Using geom_text

pl <- ggplot(data, aes(x=wt, y=mpg, label=  CarModel)) 
pl <- pl +  geom_point()

pl <- pl + theme_classic()
pl <- pl + labs(title = "Using 'geom_text' to show the labels for data points")
pl <- pl + labs(caption = 'techanswers88')
pl

# Using geom_text

pl <- ggplot(data, aes(x=wt, y=mpg, label=  CarModel)) 
pl <- pl +  geom_point()
pl <- pl +  geom_text ( nudge_x = 0.3
                      , nudge_y = 0.3
                      , check_overlap = T
                      , size = 2
                      , color = "blue"
                      , angle = 0)
pl <- pl + theme_classic()
pl <- pl + labs(title = "Using 'geom_text' to show the labels for data points")
pl <- pl + labs(caption = 'techanswers88')
pl

You can customise the labels by adding more than one data field in that, you can also use linebreak and other static text in the labels as shown below

pl <- ggplot(data, aes(x=wt, y=mpg, label=  paste0('Weight ' , wt,'\n',CarModel))) 
pl <- pl +  geom_point()
pl <- pl +  geom_text ( nudge_x = 0.3
                        , nudge_y = 0.3
                        , check_overlap = T
                        , size = 2
                        , color = "red"
                        , angle = 0)
pl <- pl + theme_classic()
pl <- pl + labs(title = "Using 'geom_text' to show the labels for data points")
pl <- pl + labs(subtitle = "putting more information in text labels")
pl <- pl + labs(caption = 'techanswers88')
pl

Using geom_label

Instead of using the geom_text you can use the geom_label option. This option gives a border around the text labels.

# Using geom_label

pl <- ggplot(data, aes(x=wt, y=mpg, label=CarModel)) 
pl <- pl +  geom_point()
pl <- pl +  geom_label ( size = 2
                        , color = "red" )
pl <- pl + theme_classic()
pl <- pl + labs(title = "Using 'geom_label' to show the labels for data points")
pl <- pl + labs(subtitle = "geom_label creates a border around the text labels")
pl <- pl + labs(caption = 'techanswers88')
pl

Using ggrepel package

ggrepel package provides geom_text_repel option. ggrepel avoids overlapping of the labels

# Using ggrepel package
library(ggrepel)

# We can use geom_text_repel using  ggrepel package

pl <- ggplot(data, aes(x=wt, y=mpg, label= CarModel)) 
pl <- pl +  geom_point()
pl <- pl +  geom_text_repel( size = 3)
pl <- pl + theme_classic()
pl <- pl + labs(title = "Using 'geom_text_repel' to show the labels for data points")
pl <- pl + labs(subtitle = "package ggrepel - controls the overlapping in a better way")
pl <- pl + labs(caption = 'techanswers88')
pl

Using ggrepel package

ggrepel package provides geom_label_repel option.

# We can use geom_label_repel using  ggrepel package

pl <- ggplot(data, aes(x=wt, y=mpg, label= CarModel)) 
pl <- pl +  geom_point()
pl <- pl +  geom_label_repel( size = 2
                              , alpha = 0.8
                              , label.padding = unit(0.25, "lines")
                              , label.size = 0.25)
pl <- pl + theme_classic()
pl <- pl + theme(legend.position="top")
pl <- pl + labs(title = "Using 'geom_label_repel' to show the labels for data points")
pl <- pl + labs(subtitle = "package ggrepel - controls the overlapping in a better way")
pl <- pl + labs(caption = 'techanswers88')
pl

Control the fill colors using the fill option

# Selective labels 
# Using fill to control the label fill colours

pl <- ggplot(data, aes(x=wt, y=mpg, label= CarModel, fill = factor(cyl))) 
pl <- pl +  geom_point()
pl <- pl +  geom_label_repel( size = 2
                              , alpha = 0.8
                               , label.padding = unit(0.25, "lines")
                               , label.size = 0.25
                              )
pl <- pl + theme_classic()
pl <- pl + labs(fill = "No. of Cylinders" )
pl <- pl + theme(legend.position="top")
pl <- pl + labs(title = "Using 'geom_label_repel' to show the labels for data points")
pl <- pl + labs(subtitle = "controlling the fill colour based on the Cylinders")
pl <- pl + labs(caption = 'techanswers88')
pl

Show labels for selected data points

# Using a criteria to control the labels to display or color


# Selecting only one data point based on the criteria by specifying the value
pl <- ggplot(data, aes(x=wt, y=mpg,label= CarModel )) 
pl <- pl +  geom_point()
pl <- pl +  geom_label_repel (
                      aes(label = ifelse(CarModel %in% 'Merc 240D',CarModel,''))
                         ,  size = 2
                        , alpha = 0.8
                        , label.padding = unit(0.25, "lines")
                        , label.size = 0.25
                      )
pl <- pl + theme_classic()
pl <- pl + labs(caption = 'techanswers88')
pl

Multiple criteria

# Selecting more than one data point based on the criteria by specifying the value
pl <- ggplot(data, aes(x=wt, y=mpg,label= CarModel )) 
pl <- pl +  geom_point()
pl <- pl +  geom_label_repel (
  aes(label = ifelse(CarModel%in% c('Merc 240D', 'Fiat 128') ,CarModel,''))
  ,  size = 2
  , alpha = 0.8
  , label.padding = unit(0.25, "lines")
  , label.size = 0.25)
pl <- pl + theme_classic()
pl <- pl + labs(caption = 'techanswers88')
pl

Controlliong the fill colour selectively

# Using a criteria to control the labels fill color

pl <- ggplot(data, aes(x=wt, y=mpg,label= CarModel )) 
pl <- pl +  geom_point()
pl <- pl +  geom_label_repel (
  aes(fill = ifelse(CarModel%in% c('Merc 240D', 'Fiat 128'), "red",'green'))
  ,  size = 2
  , alpha = 0.8
  , label.padding = unit(0.25, "lines")
  , label.size = 0.25)
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = 'none')
pl <- pl + labs(caption = 'techanswers88')
pl

Using wildcard selection to control fill colour

# Using a wildcard criteria to control the labels fill color

pl <- ggplot(data, aes(x=wt, y=mpg,label= CarModel )) 
pl <- pl +  geom_point()
pl <- pl +  geom_label_repel (
  aes(fill = ifelse(grepl("Merc", CarModel), "red",'green'))
  ,  size = 2
  , alpha = 0.8
  , label.padding = unit(0.25, "lines")
  , label.size = 0.25)
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = 'none')
pl <- pl + labs(caption = 'techanswers88')
pl