R barplot: wrapping long text labels?
There is one possible solution presented by Marc Schwartz in his post to R-help:
a <- c("I don't feel competent enough to solve problems in R", "I don't feel competent enough to solve problems in R")# Core wrapping functionwrap.it <- function(x, len){ sapply(x, function(y) paste(strwrap(y, len), collapse = "\n"), USE.NAMES = FALSE)}# Call this function with a list or vectorwrap.labels <- function(x, len){ if (is.list(x)) { lapply(x, wrap.it, len) } else { wrap.it(x, len) }}
Try it:
> wrap.labels(a, 10)[1] "I don't\nfeel\ncompetent\nenough to\nsolve\nproblems\nin R"[2] "I don't\nfeel\ncompetent\nenough to\nsolve\nproblems\nin R"
or
> wrap.labels(a, 25)[1] "I don't feel competent\nenough to solve problems\nin R"[2] "I don't feel competent\nenough to solve problems\nin R"
and then create a barplot:
wr.lap <- wrap.labels(a, 10)barplot(1:2, names.arg = wr.lap, horiz = T, las = 2, cex.names = 0.5)