wilkelab / ggtext

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

{stringr} str_wrap not working on element_markdown() text #56

Closed jesadrperez closed 3 years ago

jesadrperez commented 3 years ago

Hello,

I can no longer use stringr::str_wrap()on text that has ggtext::element_markdown() applied to it.

library(ggplot2)
library(ggtext)
library(stringr)

# Basic Plot
ggplot(mtcars, aes(mpg, hp)) +
  geom_point() +
  ggtitle("The data was extracted from the 1974 Motor Trend US magazine.")


# Plot with str_wrap
ggplot(mtcars, aes(mpg, hp)) +
  geom_point() +
  ggtitle(str_wrap("The data was extracted from the 1974 Motor Trend US magazine.", width = 50))


# Plot with str_wrap and ggtext
ggplot(mtcars, aes(mpg, hp)) +
  geom_point() +
  ggtitle(str_wrap("The data was extracted from the 1974 Motor Trend US magazine.", width = 50)) +
  theme(plot.title = element_markdown())

Created on 2020-10-19 by the reprex package (v0.3.0)

clauswilke commented 3 years ago

Yes, markdown absorbs newlines. You could use gsub() to replace \n with <br>, or you could use element_textbox() to let it handle the word wrapping. In my opinion, the second option makes more sense.