2010-04-13 14 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) 

¿Puedo establecer el título de la trama para ajustar y reducir el texto para que se ajuste a la trama?R: ggplot2, ¿puedo establecer el título del trazado para ajustarlo y reducir el texto para que se ajuste al trazado?

Respuesta

6

No creo que haya una opción de ajuste de texto en ggplot2 (siempre he insertado \ n de forma manual). Puede, sin embargo, reducir el tamaño del texto del título mediante la alteración de su código de la siguiente manera:

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

De hecho, todos los aspectos del texto con la función theme_text.

+0

'' opt' y theme_text' han cambiado de nombre: https://github.com/wch/ggplot2/wiki/New-theme-system –

+0

UPDATE: Creo que en el ggplot más reciente que se puede añadir títulos simplemente usando "\ n" –

28

Debe elegir manualmente el número de caracteres para envolver, pero la combinación de strwrap y paste hará lo que quiera.

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)) 
Cuestiones relacionadas