I'm looking for a way to dynamically wrap the strip label text in a facet_wrap or facet_grid call. I've found a way to accomplish this using strwrap, but I need to specify a width 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 width 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?
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)
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 theme settings. Maybe someone with more ggplot2 experience has some suggestions for improvement.
library('grid')
grobs <- 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))