insightsengineering / tern

Table, Listings, and Graphs (TLG) library for common outputs used in clinical trials
https://insightsengineering.github.io/tern/
Other
77 stars 22 forks source link

Integrate better wrapping functions for `decorate_grob` titles and subtitles #1248

Closed Melkiades closed 6 months ago

Melkiades commented 6 months ago

follow-up of fix #1245. The complex point here that prevents us to go all the way to the ggplot2 world is composite plots made with plot_grid (cowplot). I tried to annotate these but no wrapping is possible in anyway and this defies one important role of decorate_grob. The current wrapping method is copied from a book and uses effectively base wrapping to do it and it calculates the width with the following:

 # Main conversion of allowed text width -> npc units are 0<npc<1. External viewport is used for conversion
        if (grid::convertWidth(linewidth + gapwidth + width_i, grid::unitType(width), valueOnly = TRUE) < availwidth) {

npc is normalised to the width of the viewport and makes transforming all of this to number of characters difficult and problematic as it is already buggy for rstudio and other unclear viewports. Hence, I would not try for now to use formatters::wrap_text that keeps also spaces and respects \n.

In summary:

  1. We cant use ggplot2 decorations because plot_grid does not wrap
  2. We need to think more about using other wrapping algorithms as the current method works and others may present difficult conversions of viewport sizes