wrap_labels {ggpp} | R Documentation |
Wrap character strings in a vector
Description
Wrap the members of a character vector to a given maximum width by inserting new line characters at word boundaries.
Usage
wrap_labels(x, width = 20, indent = 0, new.line = "\n")
Arguments
x |
character vector, or an object which can be converted to a character
vector by |
width |
a positive integer giving the target column for wrapping lines in the output. |
indent |
a positive or negative integer giving the indentation of the first line in a member character string. |
new.line |
character sting; use |
Details
Function wrap_labels()
is a wrapper on link{strwrap}
that returns a vector of character strings instead of a list of vectors. In
addition to wrapping, indentation is supported. Wrapping is always at white
space, so width = 0
wraps word by word.
Because the returned value is a character vector of the same length as the
input, this function can be used within a call to aes()
when mapping a
character vector to the label
aesthetic, as long as the character
strings will not be parsed into R expressions. It can be also used to wrap
the strings in a variable stored in a data frame.
Value
A character vector of the same length as x
, with new line
characters inserted to wrap text lines longer than width
. Names in
x
are preserved in the returned value, no names are added if none
are present in x
.
Examples
my.text <- c(A = "This is the first string",
B = "This is the second string, which is longer")
wrap_labels(my.text, width = 20)
wrap_labels(unname(my.text), width = 20)
cat(wrap_labels(my.text, width = 20), sep = "\n--\n")
cat(wrap_labels(my.text, width = 20, indent = 2), sep = "\n--\n")
cat(wrap_labels(my.text, width = 20, indent = -2), sep = "\n--\n")