I'm generating a lot of ggplots in an automated fashion (for codebooks of surveys). Some of these plots have very long plot titles (often survey questions) and/or x-axis labels.
library(ggplot2)
qplot(1:10,rnorm(10)) +
ggtitle("this is indeed a very long title but sometimes necessary. this is indeed a very long title but sometimes necessary. this is indeed a very long title but sometimes necessary", subtitle = "the very long title has an even longer subtitle to boot, really much longer than the title. the very long title has an even longer subtitle to boot, really much longer than the title.") +
scale_x_continuous(breaks = 1:10, labels = paste("Option", 1:10))
Is there a way to prevent these texts from being clipped (by wrapping or shrinking or rotating them)? A way that can be automated and works for variable-width fonts? Similar questions exist, but address the old version of ggplot2, manual solutions, or facets (which seem more complicated, but there label_wrap_gen
seems to handle things well, so I thought a simple solution for titles might exist too).
I control and know the plot width and height before doing the plot (via knitr chunk settings).
I know I can wrap text using \n
or strwrap
, but there I have to choose the cut points manually and doing so for variable-width fonts often leaves empty space.