2010-04-13 40 views
15
library(ggplot2) 

my_title = "This is a really long title of a plot that I want to nicely wrap \n and fit onto the plot without having to manually add the backslash n, but at the moment it does not" 

r <- ggplot(data = cars, aes(x = speed, y = dist)) 
r + geom_smooth() + #(left) 
opts(title = my_title) 

我可以設置陰謀標題環繞和縮小文本以適應情節?R:ggplot2,我可以設置陰謀標題環繞並縮小文本以適應陰謀嗎?

回答

6

我不認爲在ggplot2中有文字換行選項(我總是手動插入\ n)。你可以,但是,通過改變下列方式代碼縮水標題文本的大小:

title.size<-10 
r + geom_smooth() + opts(title = my_title,plot.title=theme_text(size=title.size)) 

事實上,你的文字的各個方面與theme_text功能。

+0

'opt'和'theme_text'已經改名:https://github.com/wch/ggplot2/wiki/New-theme-system – 2013-02-11 20:43:09

+0

更新:我覺得在最近ggplot,您可以添加標題只需使用「\ n」 – 2013-10-21 22:27:58

28

您必須手動選擇要換行的字符數,但strwrappaste的組合將按照您的要求進行操作。

wrapper <- function(x, ...) 
{ 
    paste(strwrap(x, ...), collapse = "\n") 
} 

my_title <- "This is a really long title of a plot that I want to nicely wrap and fit onto the plot without having to manually add the backslash n, but at the moment it does not" 
r + 
    geom_smooth() + 
    opts(title = wrapper(my_title, width = 20))