As far as I know, so I applied a workaround using strwidth() , which calculates the width of the text in the base graphic.
title <- "This is a really excessively wide title for a plot, especially since it probably won't fit"
Use par("din") to get the width of the device, and strwidth () `to calculate the text size:
par("din")[1] [1] 8.819444 strwidth(title, units="inches") [1] 11.47222
Use it in function and graphics:
wrapTitle <- function(x, width=par("din")[1]){ xx <- strwrap(x, width=0.8 * nchar(x) * width / strwidth(x, units="inches")) paste(xx, collapse="\n") } wrapTitle(title) [1] "This is a really excessively wide title for a plot, especially since it\nprobably won't fit, meaning we somehow have to wrap it"
Plot:
ggplot(mtcars, aes(wt, mpg)) + geom_point() + opts(title=wrapTitle(title))

If you want to save the graph to a file, you can replace par("din") with the actual saved graph sizes.
Andrie
source share