2013-04-18 139 views
16

如果我有一個標題,進入[R GGPLOT2中心對齊多行標題

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

有沒有什麼辦法可以讓每一行居中對齊,而不是左對齊的中心?

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

給我中間的文字,但左對齊。

回答

34

將這項工作對你來說,

# 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

有關的情節標題錯別字對不起...

+1

嗨@EricFail感謝的作品。我沒有注意到,但我的問題更具體。看來只有當我使用'ggtitle(表達式(italic(somthing \ nsomething)))'時纔會出現問題''但那不是我所問的。感謝您的回答。 – 2013-04-18 07:16:17

+2

plotmath和多行不混合,這就是爲什麼你的表達問題。 – baptiste 2013-05-13 22:45:16

+1

有關詳細信息,可以查看此[文章](http://www.cookbook-r.com/Graphs/Titles_(ggplot2)/) – 2016-05-09 12:54:52