wilkelab / ggtext

Improved text rendering support for ggplot2
https://wilkelab.org/ggtext/
GNU General Public License v2.0
650 stars 37 forks source link

Working with newlines/`strwrap()`/`scales::label_wrap()` #93

Open bwiernik opened 1 year ago

bwiernik commented 1 year ago

tl;dr:

  1. Would it be possible to add arguments to replace newlines with <br> automatically in element_markdown() and geom_richtext()?
  2. Would it be possible to provide a version of scales::label_wrap() that doesn't break markdown syntax?
  3. Would it be possible to vectorize textbox_grob() so that it can be used with theme elements like axis.text.x?

Markdown treats newlines \n as a space character. This creates friction when text data is initially prepared expecting to be working with native ggplot2 functions but then the plot is changed use ggtext functions. The datas to be prepocessed to change any \n to <br>. It also makes it difficult to combine ggtext with scales::label_wrap() to automatically handle line wrapping.

Automatically replacing \n with <br> would help with the first case. Would it be possible to add an argument to element_markdown() and geom_richtext() to facilitate this transformation, either just this substitution (e.g., translate_newline) or a more general argument for transformations to to the text?

For the second case, it's trickier because naively passing a string with markdown syntax to strwrap() could break the markup across lines. One solution would be to provide a version of label_wrap() that applied the breaks after stripping the markup from the text (e.g., label_wrap_markdown()); I'm not sure how feasible that would be. Another option would be for me to switch to element_textbox() and use its built-in line wrapping. This isn't currently possible for axis text because the function textbox_grob() is not vectorized. Would it be possible to vectorize it?