R barplot:包装长文本标签? [英] R barplot: wrapping long text labels?

查看:112
本文介绍了R barplot:包装长文本标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是这个问题(水平条形图).我会在那儿问我的问题,但似乎我还没有这样做的权限.

I'm referring to this question (Automatic adjustment of margins in horizontal bar chart). I would have asked my question right there, but it seems, I don't have permission to do so yet.

想象一下一个水平条形图(如链接的问题),其中您可能会像我们在社会科学中一样经常遇到极长的标签(例如,调查项目的问题措辞,例如我感觉不足以解决问题"在R中.)

Imagine a horizontal barplot (like in the linked question) where you might have extreme long lables as we always have in social sciences (for example the question wording of a survey item like "I don't feel competent enough to solve problems in R").

用户 thelatemail 提供了一种解决方案,该解决方案如何根据标签的文本长度移动绘图的开头.可以说长度为10或15个字符的标签是可以的,但是如果必须用非常长的标签标记y轴,则无法无限期地移动图形的开始.

The user thelatemail gave a solution how to move the beginning of the plot depending on the text length of the labels. That works ok for labels with lengths of let's say 10 or 15 characters but if you have to label your y-axis with very long labels you can't move the beginning of the plot infinitly.

因此,更合适的做法是将文本标签包装在大量的单词/字符之后,例如,在我的示例中,您可能希望像这样包装它:

Thus, it is more appropriate to wrap the text labels after a spefific number of words/characters, e.g, in my example you might want to wrap it like this:

"I don't feel competent enough
to solve problems in R"

但是,我不知道如何在R中包装文本标签,而且不知道如何在自动移动图的开头时考虑到包装.例如,如果我有一个50个字符的标签,并且将其包装成2行,每行25个字符,那么 thelatemail 的解决方案将这一点考虑在内,那就太好了.

However, I don't know how to wrap the text labels in R and furthermore how to take the wrapping into account for the automatic moving of the beginning of the plot. For example, if I have a label of 50 characters and I wrap it into 2 lines of 25 characters each, then it would be great if the solution of thelatemail would take this into account.

我非常感谢您提供有关此问题的帮助! 谢谢!

I aprreciate any help regarding this problem! Thanks!

推荐答案

马克·施瓦茨(Marc Schwartz)在他的帖子中提供了一种可能的解决方案,用于

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 function
wrap.it <- function(x, len)
{ 
  sapply(x, function(y) paste(strwrap(y, len), 
                              collapse = "\n"), 
         USE.NAMES = FALSE)
}


# Call this function with a list or vector
wrap.labels <- function(x, len)
{
  if (is.list(x))
  {
    lapply(x, wrap.it, len)
  } else {
    wrap.it(x, len)
  }
}

尝试一下:

> 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"

> 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"

,然后创建一个barplot:

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)

这篇关于R barplot:包装长文本标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆