Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] allow str_wrap() to wrap into n segments of equal length #572

Closed
DanChaltiel opened this issue Sep 4, 2024 · 1 comment

Comments

@DanChaltiel
Copy link

Hi,

When using str_wrap() for plot captions, it is often useful to fix the number of lines rather than the number of characters per line.

For that, I'm using the following code:

library(tidyverse)

caption = "The dots and whiskers represent the median and the 25th and 75th percentiles of the X coefficient among the N simulated trial replicates."

p = ggplot(mtcars, aes(x=disp, y=disp)) +
  geom_point() +
  coord_fixed()  

p + labs(caption=caption) #the caption overflows

p + labs(caption=str_wrap(caption, nchar(caption)/2))

Created on 2024-09-04 with reprex v2.1.1

However, as you can see, you need to refer to the caption object twice, which is not handy, and the output is not perfect when the split happens near long words (the output has 3 lines instead of 2, don't have a workaround for that yet).

Do you think str_wrap() could support an additional argument to specify the number of lines instead of width?

@hadley
Copy link
Member

hadley commented Oct 21, 2024

str_wrap() is a pretty thin wrapper around stri_wrap(). But think the challenge will be, as you have identified, figuring out the right way to split into the correct number of lines.

@hadley hadley closed this as completed Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants