ggplot2 Tutorial

Create clear, publication-ready charts with ggplot2

Scatter plots, bar charts, histograms, boxplots and more — plus themes, labels and exporting for reports.

ggplot2 cheat sheet Get help with your graphs

ggplot2 Labels, Titles and Annotations

Updated

Good labels explain what a chart measures and why the reader should care. ggplot2 provides labs() for plot text and annotation layers for highlighting specific features.

This lesson is part of the ggplot2 tutorial. It uses the built-in mpg dataset included with ggplot2, so you can run the examples after loading the package.

Add title, subtitle and caption

labs() provides a central place to add descriptive plot text. Captions are useful for data sources or explanatory notes.

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  labs(
    title = "Engine size and fuel economy",
    subtitle = "Vehicle observations in the mpg data",
    x = "Engine displacement (litres)",
    y = "Highway fuel economy (mpg)",
    caption = "Source: ggplot2::mpg"
  )
Add title, subtitle and caption example chart created with ggplot2
Illustrative preview. Run the R code above to generate the plot from the complete ggplot2 mpg dataset.

Add text labels from data

geom_text() draws a label for each row in its layer data. Use a filtered data frame when only selected points need labels.

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_text(
    data = subset(mpg, hwy > 35),
    aes(label = model),
    vjust = -0.6,
    size = 3
  )

Use annotate for a fixed note

annotate() adds a fixed label or shape at chosen coordinates. It is useful for explaining a threshold or unusual region.

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  annotate(
    "text", x = 6, y = 35,
    label = "Higher fuel economy",
    hjust = 1
  )

Add reference lines

geom_hline(), geom_vline() and geom_abline() add reference lines. Use them to mark targets, thresholds or a fitted relationship.

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_hline(yintercept = 25,
             linetype = "dashed",
             colour = "firebrick")

Improve label readability

Keep titles informative but concise. Include units in axis labels, explain abbreviations and avoid overlapping text. For dense labels, consider ggrepel::geom_text_repel().

# install.packages("ggrepel")
library(ggrepel)

ggplot(mpg, aes(displ, hwy, label = model)) +
  geom_point() +
  geom_text_repel(
    data = subset(mpg, hwy > 35)
  )

Common mistakes and troubleshooting

  • Could not find function: run library(ggplot2) in the current R session. Install it first with install.packages("ggplot2") if needed.
  • Object not found: check spelling and confirm the column exists in the data frame. Use names(mpg) to inspect column names.
  • Unexpected legend: a variable mapped inside aes() creates a scale and usually a legend. A fixed setting belongs outside aes().
  • Plot does not appear: make sure the complete expression is evaluated. In a script or function, explicitly call print(p) when displaying a stored ggplot object.
  • Warning about removed rows: inspect missing or out-of-range values. Do not suppress the warning until you understand why observations were removed.

Next step: Continue with the next lesson using the navigation at the bottom of this page, or return to the ggplot2 tutorial overview.

ggplot2 FAQ

How do I add a title in ggplot2?

Add labs(title = "Your title") to a ggplot object.

How do I change axis labels in ggplot2?

Use labs(x = "Horizontal axis", y = "Vertical axis") to provide descriptive axis titles.

What is the difference between geom_text and annotate?

geom_text() maps labels to rows in a data frame. annotate() adds a fixed annotation that is not mapped to the main dataset.