I found this very useful code for wrapping text here:
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))
I would like to use it to wrap the text in a facet/strip but don't know how.
p + geom_bar(stat="identity")+facet_wrap(~variable1) +
opts(strip.text.x=theme_text(size=12, face="bold")
Is it passed to the strip.text.x options?
Since this question was posted, the new
label_wrap_gen()
function withggplot2
(>= 1.0.0, I think) handles this nicely:my best guess would be to define a custom theme_text for the strip label,
quick testing reveals that the width of each line doesn't necessarily fit in the facet strip, however; a better approach might be to use splitTextGrob from RGraphics where the splitting is done at drawing time to fit in the current viewport,
The problem is that ggplot expects the grob to know its size, whilst the grob expects a viewport with specific dimensions... It would generally require some sort of prior estimate, but in practical terms I don't think you want more than two lines of text.