35
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)

プロット タイトルを折り返すように設定し、プロットに合わせてテキストを縮小することはできますか?

4

3 に答える 3

61

折り返す文字数を手動で選択する必要がありますが、 と の組み合わせでstrwrap希望どおりの結果が得られpasteます。

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() + 
  ggtitle(wrapper(my_title, width = 20))
于 2010-10-14T16:34:09.953 に答える
11

にテキストの折り返しオプションがあるとは思いませんggplot2(私はいつも手動で\ n挿入しました)。ただし、次の方法でコードを変更することにより、タイトルのテキストのサイズを縮小できます。

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

実際、この関数を使用すると、テキストのすべての側面がわかりますtheme_text

于 2010-04-13T22:58:42.040 に答える