title: How to dynamically wrap facet label using ggplot2
body: <p>I'm looking for a way to dynamically wrap the strip label text in a <code>facet_wrap</code> or <code>facet_grid</code> call.  I've found a way to accomplish this using <code>strwrap</code>, but I need to specify a <code>width</code> for the output to work as desired.  Often the number of facets is not known in advance, so this method requires me to iteratively adjust the <code>width</code> parameter based on the dataset and plot size.  Is it possible to dynamically specify a width for the wrap function, or is there another option for labeling facets that would work better?</p>
  
  <pre><code>library(ggplot2)
  df = expand.grid(group=paste(c("Very Very Very Long Group Name "), 1:9),
                   x=rnorm(5), y=rnorm(5), stringsAsFactors=FALSE)
  
  df$groupwrap = unlist(lapply(strwrap(df$group, width=30, simplify=FALSE), paste, 
                               collapse="\n"))
  p = ggplot(df) +
    geom_point(aes(x=x, y=y)) +
    facet_wrap(~groupwrap)
  </code></pre>
  
  <p>UPDATE: Based on the guidance provided by @baptiste and @thunk, I came up with the option below.  Currently, it only works for a specified font family and size, but ideally, one should be able to also use the default <code>theme</code> settings. Maybe someone with more <code>ggplot2</code> experience has some suggestions for improvement.</p>
  
  <pre><code>library('grid')
  grobs &lt;- ggplotGrob(p)
  
  sum = sum(sapply(grobs$width, function(x) convertWidth(x, "in")))
  panels_width = par("din")[1] - sum  # inches
  
  df$group = as.factor(df$group)
  npanels = nlevels(df$group)
  if (class(p$facet)[1] == "wrap") {
    cols = n2mfrow(npanels)[1]
  } else {
    cols = npanels
  }
  
  ps = 12
  family = "sans"
  pad = 0.01  # inches
  panel_width = panels_width / cols
  char_width = strwidth(levels(df$group)[
    which.max(nchar(levels(df$group)))], units="inches", cex=ps / par("ps"), 
                        family=family) / max(nchar(levels(df$group)))
  width = floor((panel_width - pad)/ char_width)  # characters
  
  df$groupwrap = unlist(lapply(strwrap(df$group, width=width, simplify=FALSE), 
                               paste, collapse="\n"))
  ggplot(df) +
    geom_point(aes(x=x, y=y)) +
    facet_wrap(~groupwrap) +
    theme(strip.text.x=element_text(size=ps, family=family))
  </code></pre>