25

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.

Ferdi
  • 540
  • 3
  • 12
  • 23
Tahnoon Pasha
  • 5,848
  • 14
  • 49
  • 75

1 Answers1

49

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 …

Community
  • 1
  • 1
Eric Fail
  • 8,191
  • 8
  • 72
  • 128
  • 2
    Hi @EricFail thanks that works. I hadn't noticed but my issue is more specific. It seems the problem only arises when I use `ggtitle(expression(italic(somthing\nsomething else)))` but that wasn't what I asked. thanks for your answer. – Tahnoon Pasha Apr 18 '13 at 07:16
  • 3
    plotmath and multiline don't mix, that's why you had the issue with your expression. – baptiste May 13 '13 at 22:45
  • 1
    For details, you can check this [article](http://www.cookbook-r.com/Graphs/Titles_(ggplot2)/) – krishna Prasad May 09 '16 at 12:54