Brian Smith
Brian Smith

Reputation: 1353

Center align ggplot title when title is placed within the plot area

Let say I have below ggplot

set.seed(1)
x<-rnorm(20)
y<-rnorm(20)
df<-data.frame(x,y)
df
library(ggplot2)
ggplot(df,aes(x,y))+geom_point()+ggtitle("Scatterplot")+theme(plot.title=element_text(margin=margin(t=10,b=-20)))

With this I am getting below ggplot

enter image description here

While this is fine, I want to centre align the title without changing it's vertical position.

Is there any way to achieve this?

Upvotes: 1

Views: 927

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

theme(plot.title = element_text(hjust = 0.5, margin = margin(t=10,b=-20)))

Upvotes: 2

Related Questions