Tahnoon Pasha
Tahnoon Pasha

Reputation: 6018

R ggplot2 center align a multi-line title

If I have a title that goes

... +
ggtitle('Something\nSomething Else\nSomething Else')

Is there any way I can get each line to center align rather than left align in the center?

...+
theme(plot.title=element_text(hjust=0.5))

gives me text in the center, but left aligned.

Upvotes: 25

Views: 59230

Answers (1)

Eric Fail
Eric Fail

Reputation: 7928

would this work for you,

# install.packages("ggplot2", dependencies = TRUE)
require(ggplot2)

DF <- data.frame(x = rnorm(400))
m <- ggplot(DF, aes(x = x)) + geom_histogram()
m + labs(title = "Vehicle \n Weight-Gas \n Mileage Relationship \n 
                 and some really long so that you can seee it's centered") + 
     theme(plot.title = element_text(hjust = 0.5))

enter image description here

sorry about the typos in the plot title …

Upvotes: 50

Related Questions