How to Center Plot Title in ggplot2 with R

by | Programming, R, Tips

The easiest way to center a title using ggplot2 is to modify the plot.title component of the theme using element_text(hjust=0.5). For example,

library(ggplot2)
ggplot(data=mtcars, aes(x=mpg, y=wt)) + geom_point() + 
     ggtitle("Automobile Weight vs Miles per Gallon") +
     theme(plot.title=element_text(hjust=0.5))

This tutorial will go through how to adjust the plot title using ggplot2 with code examples.


Example

Consider the following scatterplot of two variables from the mtcars dataset miles per gallon (mpg) and weight (wt).

library(ggplot2)

# Create scatterplot using ggplot2

ggplot(data=mtcars, aes(x=mpg, y=wt)) + geom_point() + 
     ggtitle("Automobile Weight vs Miles per Gallon")

By default, plot titles in ggplot2 are left-aligned.

mtcars: wt vs mpg scatter plot, left-aligned title
mtcars: wt vs mpg scatter plot, left-aligned title

Center Aligned using hjust

We can adjust the plot theme using plot.title and element_text. The function element_text specifies the display of the text component of the plot.

The hjust argument for element_text shifts the plot title text horizontally. If we set hjust to 0.5, we centre the title. Let’s look at the revised code:

library(ggplot2)
ggplot(data=mtcars, aes(x=mpg, y=wt)) + geom_point() + 
     ggtitle("Automobile Weight vs Miles per Gallon") +
     theme(plot.title=element_text(hjust=0.5))
mtcars: wt vs mpg scatter plot, centre-aligned title
mtcars: wt vs mpg scatter plot, centre-aligned title

We can see that the title is now centre-algined.

Center Aligned using hjust and vjust

We can shift the title horizontally and vertically using the hjust and vjust arguments. If we set vjust to a negative value the title moves down the plot, if we set it to a positive value the title moves up the plot. Let’s look at the revised code:

library(ggplot2)
ggplot(data=mtcars, aes(x=mpg, y=wt)) + geom_point() + 
     ggtitle("Automobile Weight vs Miles per Gallon") +
     theme(plot.title=element_text(hjust=0.5, vjust=-7))
mtcars: wt vs mpg scatter plot, centre-aligned and vertically adjusted title
mtcars: wt vs mpg scatter plot, centre-aligned and vertically adjusted title

We can see that the title has moved down and is inside the plot.

Right Aligned using hjust and vjust

We can set right-align the title by setting hjust to 1. Let’s look at the revised code:

ggplot(data=mtcars, aes(x=mpg, y=wt)) + geom_point() + 

     ggtitle("Automobile Weight vs Miles per Gallon") +

     theme(plot.title=element_text(hjust=1, vjust=-7))
mtcars: wt vs mpg scatter plot, right-aligned and vertically adjusted title
mtcars: wt vs mpg scatter plot, right-aligned and vertically adjusted title

We can see that the title is right-aligned and shifted down inside the plot.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on R, go to the articles: 

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨