Open In App

How to Change Position of ggplot Title in R ?

Last Updated : 16 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In R, the ggplot2 package is widely used for creating data visualizations. One of the frequent customizations is rotating the title of a plot. The title for a ggplot2 plot defaults to left-alignment, but you might like to change its position according to the design or layout of the plot.

Adding a Title to a Plot with ggtitle()

The ggtitle() function in ggplot2 allows you to add a title to your plot.

Syntax:

ggtitle("title")

By Default the title is left aligned. Thus, if the requirement is a left-aligned title nothing much has to be done.

Example: Default Left-Aligned Title

R
library("ggplot2")

x<-c(1,2,3,4,5)
y<-c(10,30,20,40,35)

df<-data.frame(x,y)

ggplot(df,aes(x,y))+geom_line()+
ggtitle("Default title")

Output:

Changing the Title Position Using the theme() Function

To display the title  at any other position of the plot use theme() function. Within theme() function use plot.title parameter with element_text() function as value to it. Again within this function pass the value for hjust attribute.

Syntax: 

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

To get the title at the center the value of hjust should be assigned to 0.5.

Example: : Center-Aligned Title

R
library("ggplot2")

x<-c(1,2,3,4,5)
y<-c(10,30,20,40,35)

df<-data.frame(x,y)

ggplot(df,aes(x,y))+geom_line()+
ggtitle("Title at center")+
theme(plot.title = element_text(hjust=0.5))

Output

To display title at the right, hjust should be assigned 1 as value.

Example: Right-Aligned Title

R
library("ggplot2")

x<-c(1,2,3,4,5)
y<-c(10,30,20,40,35)

df<-data.frame(x,y)

ggplot(df,aes(x,y))+geom_line()+ggtitle("Title at right")+
theme(plot.title = element_text(hjust=1))

Output:


Next Article
Article Tags :

Similar Reads