R ggplot2中心对齐多行标题

Tah*_*sha 19 r ggplot2

如果我有一个标题

... +
ggtitle('Something\nSomething Else\nSomething Else')
Run Code Online (Sandbox Code Playgroud)

有什么方法可以让每条线对齐而不是左对齐在中心?

...+
theme(plot.title=element_text(hjust=0.5))
Run Code Online (Sandbox Code Playgroud)

给我中心的文字,但左对齐.

Eri*_*ail 39

这对你有用吗

# 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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

抱歉情节标题中的拼写错误...

  • 嗨@EricFail 谢谢你。我没有注意到,但我的问题更具体。似乎问题只出现在我使用 `ggtitle(expression(italic(somthing\nsomething else)))` 时,但这不是我问的。感谢您的回答。 (2认同)
  • plotmath和multiline不混合,这就是你表达问题的原因. (2认同)
  • 详细内容可以查看这篇【文章】(http://www.cookbook-r.com/Graphs/Titles_(ggplot2)/) (2认同)