'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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stéphane Laurent