I have an issue with wrapping long texts in ggplot2. Similar question was asked here ggplot2 is there an easy way to wrap annotation text?
My question is if we have the text like this
my_label <- "Some_arbitrarily_larger_text"
How can we shrink it using the same method ?
wrapper <- function(x, ...) paste(strwrap(x, ...), collapse = "\n")
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()+
annotate("text", x = 4, y = 25, label = wrapper(my_label, width = 5))
It seems not working for this case!
You can also call stringr::str_wrap()
, for:
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()+
annotate("text", x = 4, y = 25, label = stringr::str_wrap(my_label, 5))
I don't think either of those will break up a single word, though, if that's what you're looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With