JD *_*ong 51
尝试在标题中间添加"\n"(新行).例如:
plot(rnorm(100), main="this is my title \non two lines")
Run Code Online (Sandbox Code Playgroud)

Gre*_*now 41
您可以使用该strwrap函数将长字符串拆分为多个字符串,然后使用pastewith collapse=\n来创建要传递给主标题参数的字符串.您可能还希望使用par带mar参数的函数为边缘留出更多空间.
通过添加换行符:
plot(1:10, main=paste(rep("The quick brown fox", 3), sep="\n"))
Run Code Online (Sandbox Code Playgroud)
这将创建一个具有三条(相同)线条的图块.只需\n在你的子串之间使用.
\n在标题字符串中包含换行符/换行符(\n ),例如:
strn <- "This is a silly and overly long\ntitle that I want to use on my plot"
plot(1:10, main = strn)
Run Code Online (Sandbox Code Playgroud)
您可以使用strwrap和paste自动换行图表的标题。宽度需要适应您的介质宽度。
plot(rnorm(100), main = paste(
strwrap(
'This is a very long title wrapped on multiple lines without the need to adjust it by hand',
whitespace_only = TRUE,
width = 50
),
collapse = "\n"
))
Run Code Online (Sandbox Code Playgroud)
R 应该自动执行此操作,没有人想要裁剪标题。
这可能对任何句子都有用,因此它可以根据单词进行拆分:
wrap_sentence <- function(string, width) {
words <- unlist(strsplit(string, " "))
fullsentence <- ""
checklen <- ""
for(i in 1:length(words)) {
checklen <- paste(checklen, words[i])
if(nchar(checklen)>(width+1)) {
fullsentence <- paste0(fullsentence, "\n")
checklen <- ""
}
fullsentence <- paste(fullsentence, words[i])
}
fullsentence <- sub("^\\s", "", fullsentence)
fullsentence <- gsub("\n ", "\n", fullsentence)
return(fullsentence)
}
Run Code Online (Sandbox Code Playgroud)
我确信有一种更有效的方法可以做到这一点,但它确实可以完成这项工作。