What is the complete guide to ggplot2 titles?

The complete guide to ggplot2 titles is a comprehensive reference that provides a thorough explanation of how to effectively create and customize titles in ggplot2, a popular data visualization package in the R programming language. The guide covers various techniques for adding titles to plots, including modifying the font, size, and position of the title, as well as incorporating variables and mathematical expressions. It also includes examples and step-by-step instructions to help users create visually appealing and informative titles for their plots. This guide is an essential resource for anyone looking to master the use of titles in ggplot2 for creating compelling data visualizations.

The Complete Guide to ggplot2 Titles


The data visualization library ggplot2 makes it easy to create beautiful charts in R from scratch.

However, ggplot2 doesn’t provide a title for charts unless you specify one. This tutorial explains exactly how to add and modify titles on ggplot2 charts.

How to Add a ggplot2 Title

The following code shows how to use ggplot2 to create a grouped boxplot using the built-in iris dataset:

library(ggplot2)

ggplot(iris, aes(x=Species, y=Sepal.Length)) +
  geom_boxplot()

To add a title to the chart, we can use the ggtitle() function:

ggplot(iris, aes(x=Species, y=Sepal.Length)) +
  geom_boxplot() +
  ggtitle('Sepal Length by Species')

ggplot2 title

Note: You could also use labs(title=’Sepal Length by Species’) to create the exact same title.

How to Center a ggplot2 Title

By default, ggplot2 titles are left-aligned. The creator of ggplot2, Hadley Wickham, notes that this is because a left-aligned title works better with subtitles.

If you’d like to center a ggplot2 title, you can use this bit of code:

theme(plot.title = element_text(hjust = 0.5))

Here’s what that looks like in practice:

ggplot(iris, aes(x=Species, y=Sepal.Length)) +
  geom_boxplot() +
  ggtitle('Sepal Length by Species') +
  theme(plot.title = element_text(hjust = 0.5))

Center ggplot2 title

How to Modify the Font of a ggplot2 Title

You can modify many aspects of the font title, including:

  • family: font family
  • face: font face. Options include “italic”, “bold”, and “bold.italic”
  • color: font color
  • size: font size in pts
  • hjust: horizontal justification between 0 and 1
  • vjust: vertical justification between 0 and 1
  • lineheight: line height, i.e. the spaceing between lines for multi-line titles

Here’s an example of how to modify a few of these aspects:

ggplot(iris, aes(x=Species, y=Sepal.Length)) +
  geom_boxplot() +
  ggtitle('Sepal Length by Species') +
  theme(plot.title = element_text(hjust=0.5, color="blue", size=20, face="bold"))

Modify ggplot2 font title

How to Create a Multi-Line ggplot2 Title

If you have an unusually long title, you can simply throw in where you’d like a new line to start.  For example:

ggplot(iris, aes(x=Species, y=Sepal.Length)) +
  geom_boxplot() +
  ggtitle('Sepal Length by SpeciesnSample size (n = 150)')

Multi-line title in ggplot2

Additional Resources

A Complete Guide to the Best ggplot2 Themes
How to Create Side-by-Side Plots in ggplot2
How to Set Axis Limits in ggplot2

x