Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中包装标签文本?_R - Fatal编程技术网

如何在R中包装标签文本?

如何在R中包装标签文本?,r,R,我正在用测量数据绘制水平条形图。y轴的标签将是调查中的问题。问题相当长,因此我需要将每个问题的文本包装起来,使其显示在2-3行上。有人能分享一下如何在BaseR中最简单地做到这一点吗 counts <- c(5, 4.9, 4.4, 4.8, 4.9, 5.0, 4.9, 4.9, 4.9) barplot(counts, col=c("deepskyblue2"), border = NA, family="Arial", horiz = T, xlim = ran

我正在用测量数据绘制水平条形图。y轴的标签将是调查中的问题。问题相当长,因此我需要将每个问题的文本包装起来,使其显示在2-3行上。有人能分享一下如何在BaseR中最简单地做到这一点吗

counts <- c(5, 4.9, 4.4, 4.8, 4.9, 5.0, 4.9, 4.9, 4.9)

barplot(counts, col=c("deepskyblue2"), border = NA,  
        family="Arial", horiz = T, xlim = range(0,6))

我需要每一个的标签都能绕2-3行。

这是一个非常原始的解决方案,但不是完全基于R:

counts <- c(5, 4.9, 4.4, 4.8, 4.9, 5.0, 4.9, 4.9, 4.9)
library(stringr) # for the function str_wrap()
library(magrittr) # just for the pipe %>%, not strictly necessary
names(counts) <- c(
  "I'm working on a horizontal",
  "barplot with survey data.",
  "The labels for the y axis",
  "will be questions from the survey.",
  "The questions are rather long, and",
  "therefore I need to wrap the text",
  "of each question so that it appears",
  "on 2-3 lines. Can someone share",
  "how to most simply do this in base R?") %>% str_wrap(width = 20)

par(mai=c(1,2,1,1)) # make space for the label
barplot(counts, col=c("deepskyblue2"), border = NA, family="Arial", horiz = T, xlim = range(0,6), las = 2)

这是一个非常原始的解决方案,但不是完全基于R:

counts <- c(5, 4.9, 4.4, 4.8, 4.9, 5.0, 4.9, 4.9, 4.9)
library(stringr) # for the function str_wrap()
library(magrittr) # just for the pipe %>%, not strictly necessary
names(counts) <- c(
  "I'm working on a horizontal",
  "barplot with survey data.",
  "The labels for the y axis",
  "will be questions from the survey.",
  "The questions are rather long, and",
  "therefore I need to wrap the text",
  "of each question so that it appears",
  "on 2-3 lines. Can someone share",
  "how to most simply do this in base R?") %>% str_wrap(width = 20)

par(mai=c(1,2,1,1)) # make space for the label
barplot(counts, col=c("deepskyblue2"), border = NA, family="Arial", horiz = T, xlim = range(0,6), las = 2)

有两种可能性。使用\n在新向量中添加换行符或生成较小的文本。这样做是否有效:?两种可能性。使用\n在新向量中添加换行符或生成较小的文本。这样做是否有效:?在基R中有一个strwrap函数;你能用这个吗?@BenBolker谢谢,非常有用的发现!在base R中有一个strwrap函数;你能用这个吗?@BenBolker谢谢,非常有用的发现!