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.
Table of contents
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.
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))
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))
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))
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:
- How to Transpose a Data Frame in R
- How to Apply a Function to Every Row of a Table in R using dplyr
- How to Remove Legend in ggplot2
- How to Solve R Error: ggplot2 doesn’t know how to deal with data of class uneval
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!
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.