4

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.

clipped text on plot

Ruben
  • 3,452
  • 31
  • 47
  • simply delete the `theme_bw(base_size = 30)`. thats it. For the X axis i would use stringr's `str_wrap` like `scale_x_continuous(breaks = 1:10,labels = str_wrap(paste("Option", 1:10), width = 3))` – Roman Oct 12 '17 at 09:51
  • @Jimbou The base_size is just to illustrate the problem with less text. Obviously, this occurs also with smaller size and more text. `str_wrap` is not useful for dynamic programming if I don't know the number of labels. – Ruben Oct 12 '17 at 09:54
  • then revise your reproducible example to be more precise. – Roman Oct 12 '17 at 09:55
  • @Jimbou I bolded the __automated__ part and duplicated text. – Ruben Oct 12 '17 at 10:18
  • I think you can do it directly with `\n` while you are typing. –  Oct 12 '17 at 11:32
  • @Alice I know, but I want to automate this for hundreds of plots. – Ruben Oct 12 '17 at 11:48
  • I found an answer that can automate this at [this link](https://stackoverflow.com/questions/2631780/r-ggplot2-can-i-set-the-plot-title-to-wrap-around-and-shrink-the-text-to-fit-t?noredirect=1&lq=1) posted by OP above. Scroll past the accepted answer from 2010 down to @Richie 's answer. EDIT: It's 2018; I added this in case people are still looking for a solution and missed the solution by not scrolling down like I did. – armipunk Aug 29 '18 at 20:36
  • @armipunk That's not automated to fit with a variable-width font, it will only work with fixed-width. Note that I mention this non-solution in my question already. – Ruben Aug 30 '18 at 07:42
  • @Ruben My mistake. I was focused on the question in the title (which is what I was looking to solve) and overlooked "variable-width" in the description. Also, I saw your comment after the non-solution link re 'old version' - and was wondering why you didn't just change 'opts' to 'labs'. If you can still edit your post - bold variable-width? Did you find a solution eventually? – armipunk Aug 30 '18 at 17:32

0 Answers0